Bayesian GLM Part4

Author

Murray Logan

Published

07/07/2025

1 Preparations

Load the necessary libraries

library(tidyverse) # for data wrangling etc
library(rstanarm) # for fitting models in STAN
library(cmdstanr) # for cmdstan
library(brms) # for fitting models in STAN
library(coda) # for diagnostics
library(bayesplot) # for diagnostics
library(ggmcmc) # for MCMC diagnostics
library(DHARMa) # for residual diagnostics
library(rstan) # for interfacing with STAN
library(emmeans) # for marginal means etc
library(broom) # for tidying outputs
library(tidybayes) # for more tidying outputs
library(ggeffects) # for partial plots
library(broom.mixed) # for summarising models
library(ggeffects) # for partial effects plots
library(bayestestR) # for ROPE
library(see) # for some plots
library(easystats) # for the easystats ecosystem
library(patchwork) # for multiple plots
library(modelsummary) # for data and model summaries
library(car) # for scatterplot matrices
library(ggridges) # for ridge plots
theme_set(theme_grey()) # put the default ggplot theme back
source("helperFunctions.R")

2 Scenario

Loyn (1987) modelled the abundance of forest birds with six predictor variables (patch area, distance to nearest patch, distance to nearest larger patch, grazing intensity, altitude and years since the patch had been isolated).

Figure 1: Regent honeyeater
Table 1: Format of loyn.csv data file
ABUND DIST LDIST AREA GRAZE ALT YR.ISOL
.. .. .. .. .. .. ..
Table 2: Description of the variables in the loyn data file
ABUND Abundance of forest birds in patch- response variable
DIST Distance to nearest patch - predictor variable
LDIST Distance to nearest larger patch - predictor variable
AREA Size of the patch - predictor variable
GRAZE Grazing intensity (1 to 5, representing light to heavy) - predictor variable
ALT Altitude - predictor variable
YR.ISOL Number of years since the patch was isolated - predictor variable

The aim of the analysis is to investigate the effects of a range of predictors on the abundance of forest birds.

3 Read in the data

loyn <- read_csv("../data/loyn.csv", trim_ws = TRUE)
Rows: 56 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (7): ABUND, AREA, YR.ISOL, DIST, LDIST, GRAZE, ALT

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
loyn |> glimpse()
Rows: 56
Columns: 7
$ ABUND   <dbl> 5.3, 2.0, 1.5, 17.1, 13.8, 14.1, 3.8, 2.2, 3.3, 3.0, 27.6, 1.8…
$ AREA    <dbl> 0.1, 0.5, 0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.…
$ YR.ISOL <dbl> 1968, 1920, 1900, 1966, 1918, 1965, 1955, 1920, 1965, 1900, 19…
$ DIST    <dbl> 39, 234, 104, 66, 246, 234, 467, 284, 156, 311, 66, 93, 39, 40…
$ LDIST   <dbl> 39, 234, 311, 66, 246, 285, 467, 1829, 156, 571, 332, 93, 39, …
$ GRAZE   <dbl> 2, 5, 5, 3, 5, 3, 5, 5, 4, 5, 3, 5, 2, 1, 5, 5, 3, 3, 3, 2, 2,…
$ ALT     <dbl> 160, 60, 140, 160, 140, 130, 90, 60, 130, 130, 210, 160, 210, …
loyn |> str()
spc_tbl_ [56 × 7] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
 $ ABUND  : num [1:56] 5.3 2 1.5 17.1 13.8 14.1 3.8 2.2 3.3 3 ...
 $ AREA   : num [1:56] 0.1 0.5 0.5 1 1 1 1 1 1 1 ...
 $ YR.ISOL: num [1:56] 1968 1920 1900 1966 1918 ...
 $ DIST   : num [1:56] 39 234 104 66 246 234 467 284 156 311 ...
 $ LDIST  : num [1:56] 39 234 311 66 246 ...
 $ GRAZE  : num [1:56] 2 5 5 3 5 3 5 5 4 5 ...
 $ ALT    : num [1:56] 160 60 140 160 140 130 90 60 130 130 ...
 - attr(*, "spec")=
  .. cols(
  ..   ABUND = col_double(),
  ..   AREA = col_double(),
  ..   YR.ISOL = col_double(),
  ..   DIST = col_double(),
  ..   LDIST = col_double(),
  ..   GRAZE = col_double(),
  ..   ALT = col_double()
  .. )
 - attr(*, "problems")=<externalptr> 
## Explore the first 6 rows of the data
loyn |> head()
# A tibble: 6 × 7
  ABUND  AREA YR.ISOL  DIST LDIST GRAZE   ALT
  <dbl> <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
1   5.3   0.1    1968    39    39     2   160
2   2     0.5    1920   234   234     5    60
3   1.5   0.5    1900   104   311     5   140
4  17.1   1      1966    66    66     3   160
5  13.8   1      1918   246   246     5   140
6  14.1   1      1965   234   285     3   130
loyn |> datawizard::data_codebook()
loyn (56 rows and 7 variables, 7 shown)

ID | Name    | Type    | Missings |       Values |          N
---+---------+---------+----------+--------------+-----------
1  | ABUND   | numeric | 0 (0.0%) |  [1.5, 39.6] |         56
---+---------+---------+----------+--------------+-----------
2  | AREA    | numeric | 0 (0.0%) |  [0.1, 1771] |         56
---+---------+---------+----------+--------------+-----------
3  | YR.ISOL | numeric | 0 (0.0%) | [1890, 1976] |         56
---+---------+---------+----------+--------------+-----------
4  | DIST    | numeric | 0 (0.0%) |   [26, 1427] |         56
---+---------+---------+----------+--------------+-----------
5  | LDIST   | numeric | 0 (0.0%) |   [26, 4426] |         56
---+---------+---------+----------+--------------+-----------
6  | GRAZE   | numeric | 0 (0.0%) |            1 | 13 (23.2%)
   |         |         |          |            2 |  8 (14.3%)
   |         |         |          |            3 | 15 (26.8%)
   |         |         |          |            4 |  7 (12.5%)
   |         |         |          |            5 | 13 (23.2%)
---+---------+---------+----------+--------------+-----------
7  | ALT     | numeric | 0 (0.0%) |    [60, 260] |         56
-------------------------------------------------------------
loyn |> modelsummary::datasummary_skim(categorical = TRUE)
Unique Missing Pct. Mean SD Min Median Max Histogram
ABUND 54 0 19.5 10.7 1.5 21.0 39.6
AREA 36 0 69.3 266.1 0.1 7.5 1771.0
YR.ISOL 25 0 1949.8 25.6 1890.0 1962.5 1976.0
DIST 31 0 240.4 219.1 26.0 234.0 1427.0
LDIST 46 0 733.3 916.1 26.0 338.5 4426.0
GRAZE 5 0 3.0 1.5 1.0 3.0 5.0
ALT 20 0 146.2 43.5 60.0 140.0 260.0

4 Exploratory data analysis

When we explored this analysis from a frequentist perspective, we decided on a log-normal like model. This was a model that was fit against a Gaussian distribution, yet with a log-link. We will replicate that model here in a Bayesian framework.

In the previous exploration of this model, we elected to treat Grazing intensity as a categorical variable - we will again code Grazing intensity as a categorical variable.

loyn <- loyn |> mutate(fGRAZE = factor(GRAZE))

Model formula: \[ y_i \sim{} \mathcal{N}(\mu_i, \sigma^2)\\ log(\mu_i) = \boldsymbol{\beta} \bf{X_i}\\ \beta_0 \sim{} \mathcal{N}(3,0.5)\\ \beta_{1-9} \sim{} \mathcal{N}(0,2.5)\\ \sigma \sim{} \mathcal{Gamma}(2,1)\\ OR\\ \sigma \sim{} \mathcal{t}(3,0,2.5) \]

where \(\boldsymbol{\beta}\) is a vector of effects parameters and \(\bf{X}\) is a model matrix representing the additive effects of the scaled versions of distance (ln), distance to the nearest large patch (ln), patch area (ln), grazing intensity, year of isolation and altitude on the abundance of forest birds.

4.1 Scatterplot matrix

To re-acquaint ourselves with the data, I we will revisit the scatterplot matrix that we generated prior to the frequentist analysis.

scatterplotMatrix(~ ABUND + DIST + LDIST + AREA + GRAZE + ALT + YR.ISOL,
  data = loyn,
  diagonal = list(method = "boxplot")
)

We again notice that DIST, LDIST and AREA are skewed, so we will normalise them via a logarithmic transformation.

scatterplotMatrix(~ ABUND + log(DIST) + log(LDIST) + log(AREA) + GRAZE + ALT + YR.ISOL,
  data = loyn,
  diagonal = list(method = "boxplot")
)

5 Fit the model

loyn.glm <- glm(
  ABUND ~ scale(log(DIST)) + scale(log(LDIST)) + scale(log(AREA)) +
    fGRAZE + scale(ALT) + scale(YR.ISOL),
  data = loyn,
  family = gaussian(link = "log")
)
loyn.glm |> summary()

Call:
glm(formula = ABUND ~ scale(log(DIST)) + scale(log(LDIST)) + 
    scale(log(AREA)) + fGRAZE + scale(ALT) + scale(YR.ISOL), 
    family = gaussian(link = "log"), data = loyn)

Coefficients:
                   Estimate Std. Error t value Pr(>|t|)    
(Intercept)        3.092248   0.112856  27.400  < 2e-16 ***
scale(log(DIST))  -0.018067   0.057247  -0.316  0.75373    
scale(log(LDIST))  0.057086   0.059984   0.952  0.34623    
scale(log(AREA))   0.203976   0.059620   3.421  0.00132 ** 
fGRAZE2            0.039644   0.148978   0.266  0.79134    
fGRAZE3            0.019654   0.137752   0.143  0.88717    
fGRAZE4           -0.001197   0.156199  -0.008  0.99392    
fGRAZE5           -1.121563   0.343631  -3.264  0.00208 ** 
scale(ALT)        -0.003237   0.048607  -0.067  0.94719    
scale(YR.ISOL)    -0.018246   0.074404  -0.245  0.80737    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 42.40928)

    Null deviance: 6337.9  on 55  degrees of freedom
Residual deviance: 1950.8  on 46  degrees of freedom
AIC: 379.76

Number of Fisher Scoring iterations: 6

In rstanarm, the default priors are designed to be weakly informative. They are chosen to provide moderate regularisation (to help prevent over-fitting) and help stabilise the computations.

loyn.rstanarm <- stan_glm(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  data = loyn,
  family = gaussian(link = "log"),
  iter = 5000, warmup = 2500,
  chains = 3, thin = 5, refresh = 0
)
Warning: There were 354 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
Warning: There were 3 chains where the estimated Bayesian Fraction of Missing Information was low. See
https://mc-stan.org/misc/warnings.html#bfmi-low
Warning: Examine the pairs() plot to diagnose sampling problems
Warning: The largest R-hat is 5.88, indicating chains have not mixed.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#r-hat
Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
Warning: Markov chains did not converge! Do not analyze results!

Conclusions:

  • the warning messages suggest that this model has not performed well
    • there are a large number of divergent transitions. Divergent transitions occur when the sampler is in a ‘sharp’ part of the posterior and the step length is too large causing the sampler to shoot off the posterior and reject the sample. This leads to inefficient and ineffective MCMC sampling. To alleviate this, we can either:
      • review the model itself - it might be misspecified
      • adjust the adaptive delta - a parameter that governs the degree of step-length learning that occurs during the warmup stage - the more it learns the less likely the sampler will be to overshoot (although it will take longer to learn)
      • review the priors
    • the largest R-hat value is large. This suggests that the chains have not converged well
    • the effective sample sizes is too low, suggesting that there might be issues with the priors.
loyn.rstanarm <- stan_glm(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  data = loyn,
  family = gaussian(link = "log"),
  iter = 5000, warmup = 2500,
  chains = 3, thin = 5, refresh = 0,
  adapt_delta = 0.99
)
Warning: There were 766 divergent transitions after warmup. See
https://mc-stan.org/misc/warnings.html#divergent-transitions-after-warmup
to find out why this is a problem and how to eliminate them.
Warning: There were 3 chains where the estimated Bayesian Fraction of Missing Information was low. See
https://mc-stan.org/misc/warnings.html#bfmi-low
Warning: Examine the pairs() plot to diagnose sampling problems
Warning: The largest R-hat is 5.13, indicating chains have not mixed.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#r-hat
Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#bulk-ess
Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable.
Running the chains for more iterations may help. See
https://mc-stan.org/misc/warnings.html#tail-ess
Warning: Markov chains did not converge! Do not analyze results!

There are still a substantial number of divergent transitions. It is possible that the priors are too vague.

prior_summary(loyn.rstanarm)
Priors for model 'loyn.rstanarm' 
------
Intercept (after predictors centered)
  Specified prior:
    ~ normal(location = 0, scale = 2.5)
  Adjusted prior:
    ~ normal(location = 0, scale = 27)

Coefficients
  Specified prior:
    ~ normal(location = [0,0,0,...], scale = [2.5,2.5,2.5,...])
  Adjusted prior:
    ~ normal(location = [0,0,0,...], scale = [26.84,26.84,26.84,...])

Auxiliary (sigma)
  Specified prior:
    ~ exponential(rate = 1)
  Adjusted prior:
    ~ exponential(rate = 0.093)
------
See help('prior_summary.stanreg') for more details

Conclusions:

  • the default priors appear to be overly wide. We will instead define our own priors. ### Assessing priors
loyn.rstanarm1 <- update(loyn.rstanarm, prior_PD = TRUE)
ggemmeans(loyn.rstanarm1, ~AREA) |> plot(show_data = TRUE) + scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

ggpredict(loyn.rstanarm1) |>
  plot(show_data = TRUE) |>
  wrap_plots() &
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

Conclusions:

  • we see that the range of predictions are extremely wide and the slope could range from strongly negative to strongly positive.
  • \(\beta_0\): normal centred at 3 with a standard deviation of 1 (on the log scale, these are the equivalent of 20.1 and 1 respectively)
    • mean of 3: since mean(log(loyn$ABUND))
    • sd of 1: since sd(log(loyn$ABUND))
  • \(\beta_1\): normal centred at 0 with a standard deviation of 2.5 (again, consider what this would be on a response scale)
    • sd of 1: since sd(log(loyn$ABUND))/sd(scale(log(loyn$AREA))) (and since each predictor is scaled, it will be the same for each predictor)
  • \(\sigma\): half cauchy (flat Gaussian) prior with a scale of 2.

I will also overlay the raw data for comparison.

loyn.rstanarm2 <- stan_glm(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  data = loyn,
  family = gaussian(link = "log"),
  prior_intercept = normal(3, 1, autoscale = FALSE),
  prior = normal(0, 1, autoscale = FALSE),
  prior_aux = cauchy(0, 2),
  prior_PD = TRUE,
  iter = 5000, thin = 5,
  chains = 3, warmup = 2500,
  refresh = 0
)

Conclusions:

  • there are no longer any warning messages.
ggemmeans(loyn.rstanarm2, ~AREA) |>
  plot(show_data = TRUE) + scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

ggpredict(loyn.rstanarm2) |>
  plot(show_data = TRUE) |>
  wrap_plots() &
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

Now lets refit, conditioning on the data.

loyn.rstanarm3 <- update(loyn.rstanarm2, prior_PD = FALSE)
posterior_vs_prior(loyn.rstanarm3,
  color_by = "vs", group_by = TRUE,
  facet_args = list(scales = "free_y")
)

Drawing from prior...

Conclusions:

  • in each case, the prior is substantially wider than the posterior, suggesting that the posterior is not biased towards the prior.
# ggemmeans(loyn.rstanarm3,  ~AREA) |> plot(show_data=TRUE) + scale_y_log10()
ggpredict(loyn.rstanarm3, terms = "AREA[0:1000]") |>
  plot(jitter = FALSE, show_data = TRUE) +
  scale_x_log10() +
  scale_x_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for x is already present.
Adding another scale for x, which will replace the existing scale.
Warning in scale_x_log10(): log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.

## ggpredict(loyn.rstanarm3,  terms = "AREA[0:1000]") |>
##     plot(jitter = FALSE, residuals=TRUE, log.y = TRUE) + scale_x_log10()
ggpredict(loyn.rstanarm3) |>
  plot(show_data = TRUE) |>
  wrap_plots() &
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

Note that for the second of the above are partial plots (effect of one predictor at the average of the continuous predictors and the first level of the categorical predictor), the raw data are not similarly standardised and thus may appear not to match the trends…

In brms, the default priors are designed to be weakly informative. They are chosen to provide moderate regularisation (to help prevent over-fitting) and help stabilise the computations.

Unlike rstanarm, brms models must be compiled before they start sampling. For most models, the compilation of the stan code takes around 45 seconds.

loyn.form <- bf(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  family = gaussian(link = "log")
)
loyn.brm <- brm(loyn.form,
  data = loyn,
  iter = 5000,
  warmup = 2500,
  chains = 3, cores = 3,
  thin = 5,
  refresh = 0,
  backend = "rstan"
)
Compiling Stan program...
Start sampling
loyn.brm |> prior_summary()
                 prior     class          coef group resp dpar nlpar lb ub       source
                (flat)         b                                                default
                (flat)         b       fGRAZE2                             (vectorized)
                (flat)         b       fGRAZE3                             (vectorized)
                (flat)         b       fGRAZE4                             (vectorized)
                (flat)         b       fGRAZE5                             (vectorized)
                (flat)         b      scaleALT                             (vectorized)
                (flat)         b  scalelogAREA                             (vectorized)
                (flat)         b  scalelogDIST                             (vectorized)
                (flat)         b scalelogLDIST                             (vectorized)
                (flat)         b  scaleYR.ISOL                             (vectorized)
  student_t(3, 3, 2.5) Intercept                                                default
 student_t(3, 0, 11.8)     sigma                                      0         default
priors <- prior(normal(0, 2.5), class = "b")
loyn.form <- bf(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  family = gaussian(link = "log")
)
loyn.brm1 <- brm(loyn.form,
  data = loyn,
  prior = priors,
  sample_prior = "only",
  iter = 5000,
  warmup = 2500,
  chains = 3,
  thin = 5,
  refresh = 0,
  backend = "rstan"
)
Compiling Stan program...
Start sampling
## Individual plots - the following seems to be broken??
## loyn.brm1 |> ggemmeans(~AREA) |> plot(show_data = TRUE) + scale_y_log10()
loyn.brm1 |>
  ggemmeans(~AREA) |>
  plot(show_data = TRUE) + scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

## All effects
loyn.brm1 |>
  conditional_effects() |>
  plot(points = TRUE, ask = FALSE, plot = FALSE) |>
  wrap_plots() &
  scale_y_log10()

loyn.brm1 |>
  ggpredict() |>
  plot(show_data = TRUE, facet = TRUE) +
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.

Conclusions:

  • we see that the range of predictions is fairly wide and the slope could range from strongly negative to strongly positive.
  • \(\beta_0\): normal centred at 3 with a standard deviation of 1 (on the log scale, these are the equivalent of 20.1 and 1 respectively)
    • mean of 3: since median(log(loyn$ABUND))
    • sd of 0.5: since mad(log(loyn$ABUND))
  • \(\beta_1\): normal centred at 0 with a standard deviation of 2.5 (again, consider what this would be on a response scale)
    • sd of 1: since mad(log(loyn$ABUND))/mad(scale(log(loyn$AREA))) (and since each predictor is scaled, it will be the same for each predictor)
  • \(\sigma\): half cauchy (flat Gaussian) prior with a scale of 1.
mod.mat <- model.matrix(as.formula(loyn.form), data = loyn)
mad(log(loyn$ABUND)) /
  apply(mod.mat, 2, mad)
      (Intercept)  scale(log(DIST)) scale(log(LDIST))  scale(log(AREA)) 
              Inf         0.6280229         0.5626313         0.5009688 
          fGRAZE2           fGRAZE3           fGRAZE4           fGRAZE5 
              Inf               Inf               Inf               Inf 
       scale(ALT)    scale(YR.ISOL) 
        0.5127373         1.3907494 
priors <- prior(normal(3.4, 0.1), class = "Intercept") +
  prior(normal(0, 1.5), class = "b") +
  ## prior(gamma(1, 1), class = 'sigma')
  prior(student_t(3, 0, 1.5), class = "sigma")
loyn.form <- bf(
  ABUND ~ scale(log(DIST)) +
    scale(log(LDIST)) +
    scale(log(AREA)) +
    fGRAZE +
    scale(ALT) +
    scale(YR.ISOL),
  family = gaussian(link = "log")
)
## family = lognormal())
loyn.brm2 <- brm(loyn.form,
  data = loyn,
  prior = priors,
  sample_prior = "only",
  iter = 5000,
  warmup = 2500,
  chains = 3, cores = 3,
  thin = 5,
  refresh = 0,
  backend = "rstan"
)
Compiling Stan program...
Start sampling
loyn.brm2 |>
  ggemmeans(~DIST) |>
  plot(show_data = TRUE) +
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

loyn.brm2 |>
  conditional_effects() |>
  plot(points = TRUE, ask = FALSE, plot = FALSE) |>
  ## lapply(function(x) x + scale_y_log10()) |>
  wrap_plots() &
  scale_y_log10()

loyn.brm2 |>
  ggpredict() |>
  plot(show_data = TRUE, facet = TRUE) +
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.

loyn.brm3 <- update(loyn.brm2, sample_prior = "yes", refresh = 0)
The desired updates require recompiling the model
Compiling Stan program...
Start sampling
loyn.brm3 |> get_variables()
 [1] "b_Intercept"     "b_scalelogDIST"  "b_scalelogLDIST" "b_scalelogAREA" 
 [5] "b_fGRAZE2"       "b_fGRAZE3"       "b_fGRAZE4"       "b_fGRAZE5"      
 [9] "b_scaleALT"      "b_scaleYR.ISOL"  "sigma"           "Intercept"      
[13] "prior_Intercept" "prior_b"         "prior_sigma"     "lprior"         
[17] "lp__"            "accept_stat__"   "stepsize__"      "treedepth__"    
[21] "n_leapfrog__"    "divergent__"     "energy__"       
## loyn.brm3 |> hypothesis('Intercept = 0', class = 'b') |> plot
## loyn.brm3 |> hypothesis('Intercept = 0', class = 'prior') |> plot
loyn.brm3 |>
  hypothesis("scalelogDIST = 0") |>
  plot()

loyn.brm3 |>
  hypothesis("scalelogAREA = 0") |>
  plot()

loyn.brm3 |>
  hypothesis("sigma = 0", class = "") |>
  plot()

loyn.brm3 |> SUYR_prior_and_posterior()

loyn.brm3 |> standata()
$N
[1] 56

$Y
 [1]  5.3  2.0  1.5 17.1 13.8 14.1  3.8  2.2  3.3  3.0 27.6  1.8 21.2 14.6  8.0
[16]  3.5 29.0  2.9 24.3 19.4 24.4  5.0 15.8 25.3 19.5 20.9 16.3 18.8 19.9 13.0
[31]  6.8 21.7 27.8 26.8 16.6 30.4 11.5 26.0 25.7 12.7 23.5 24.9 29.0 28.3 28.3
[46] 32.0 37.7 39.6 29.6 31.0 34.4 27.3 30.5 33.0 29.5 30.9

$K
[1] 10

$Kc
[1] 9

$X
   Intercept scalelogDIST scalelogLDIST scalelogAREA fGRAZE2 fGRAZE3 fGRAZE4
1          1  -1.51019511   -1.65892116  -2.37802367       1       0       0
2          1   0.37029448   -0.30532977  -1.51765965       0       0       0
3          1  -0.48079400   -0.09042449  -1.51765965       0       0       0
4          1  -0.95804924   -1.26148217  -1.14712103       0       1       0
5          1   0.42278148   -0.26754922  -1.14712103       0       0       0
6          1   0.37029448   -0.15637842  -1.14712103       0       1       0
7          1   1.09552219    0.21669491  -1.14712103       0       0       0
8          1   0.57353754    1.24803687  -1.14712103       0       0       0
9          1  -0.05524976   -0.61163991  -1.14712103       0       0       1
10         1   0.66885367    0.36858640  -1.14712103       0       0       0
11         1  -0.95804924   -0.04106159  -0.77658241       0       1       0
12         1  -0.59812145   -1.00240327  -0.77658241       0       0       0
13         1  -1.51019511   -1.65892116  -0.77658241       1       0       0
14         1   0.93822292    0.10346964  -0.77658241       0       0       0
15         1   0.47682817   -0.22864597  -0.77658241       0       0       0
16         1  -0.24660010    0.43442972  -0.77658241       0       0       0
17         1  -1.93573935   -1.96523129  -0.55983122       0       1       0
18         1  -1.93573935   -1.96523129  -0.55983122       0       1       0
19         1  -1.48362354   -1.63979473  -0.40604380       0       1       0
20         1   0.47682817   -0.22864597  -0.40604380       1       0       0
21         1   0.37029448    0.29645165  -0.40604380       1       0       0
22         1  -1.93573935    1.38927509  -0.40604380       0       0       0
23         1  -1.51019511   -1.65892116  -0.28675700       0       1       0
24         1   0.85682391    0.04487798  -0.28675700       0       0       0
25         1  -0.59812145   -0.33160908  -0.18929260       0       1       0
26         1  -0.03525827    0.79868571  -0.18929260       0       1       0
27         1   0.57722655    0.69705984  -0.10688762       0       1       0
28         1  -0.22265561   -0.73213997  -0.10688762       0       0       1
29         1   0.50481706   -0.20849935  -0.03550518       0       0       1
30         1   0.79284436    1.26397616   0.02745860       0       0       0
31         1   0.75311975    0.29645165   0.08378161       0       1       0
32         1  -1.89613008   -1.93672022   0.13473198       0       0       1
33         1  -0.03525827   -0.59724988   0.18124602       0       0       1
34         1  -0.22265561   -0.73213997   0.18124602       0       0       1
35         1  -0.12477989   -0.66168825   0.22403479       1       0       0
36         1  -0.12477989    0.09591504   0.30053281       0       1       0
37         1   0.90372228    1.51230754   0.36744180       0       0       0
38         1  -1.48362354    1.66778539   0.39799721       1       0       0
39         1   0.50481706    0.99128245   0.42690016       0       0       1
40         1   0.66885367    1.53439756   0.50527059       0       0       0
41         1   1.35327186    0.40222517   0.59457340       0       0       0
42         1   1.25762760    0.59446805   0.65294853       0       1       0
43         1   0.24667868   -0.39430941   0.70557205       0       0       0
44         1  -0.95804924   -0.01204503   0.73798041       0       0       0
45         1   0.57722655   -0.51197475   0.82485885       1       0       0
46         1  -0.59812145   -1.00240327   0.87580921       0       1       0
47         1   0.47682817    0.98837574   0.92232325       0       1       0
48         1   2.26783778    1.12640241   0.93334579       0       0       0
49         1   0.92772762    1.07832552   0.94414564       0       0       0
50         1   1.09552219    0.21669491   1.01418997       0       0       0
51         1  -1.51019511    0.29645165   1.29286187       1       0       0
52         1   0.93822292    1.91565164   1.35582564       0       0       0
53         1   1.09552219    1.39201101   1.47113789       0       0       0
54         1  -0.12477989   -0.07123733   1.50961306       0       0       0
55         1   0.75311975    1.00336997   2.53095496       0       0       0
56         1   0.73743154   -0.04106159   2.85111978       0       0       0
   fGRAZE5    scaleALT scaleYR.ISOL
1        0  0.31588146    0.7134084
2        1 -1.98143824   -1.1629534
3        1 -0.14358248   -1.9447708
4        0  0.31588146    0.6352266
5        1 -0.14358248   -1.2411351
6        0 -0.37331445    0.5961358
7        1 -1.29224233    0.2052271
8        1 -1.98143824   -1.1629534
9        0 -0.37331445    0.5961358
10       1 -0.37331445   -1.9447708
11       0  1.46454131   -0.9284082
12       1  0.31588146   -2.3356795
13       0  1.46454131    0.9088627
14       0  1.46454131    0.8697719
15       1 -0.60304642   -1.9447708
16       1 -0.02871650   -1.9447708
17       0 -0.83277839    0.4788632
18       0 -0.14358248    0.5961358
19       0  1.00507737    0.4006814
20       0 -1.29224233    0.1270453
21       0  1.69427328    0.9088627
22       1 -0.60304642   -1.0456808
23       0 -0.37331445    0.5961358
24       0 -1.06251036    0.6743175
25       0  0.54561343   -2.3356795
26       0  0.08614949    0.4006814
27       0 -0.37331445    0.5961358
28       0  1.46454131    0.4006814
29       0 -0.60304642    0.9088627
30       1 -1.29224233   -1.5538621
31       0 -0.83277839    0.4788632
32       0  0.66047941    0.4006814
33       0 -0.83277839    0.5179540
34       0  1.23480934    0.4006814
35       0  1.00507737    0.7134084
36       0 -0.60304642    0.6352266
37       1 -1.06251036   -1.1629534
38       0  1.00507737    0.6352266
39       0  0.08614949    0.9088627
40       1 -1.29224233   -1.2411351
41       0 -0.14358248    0.5179540
42       0 -0.37331445    0.5961358
43       0  1.00507737    0.9479536
44       0 -0.83277839    0.5961358
45       0 -0.60304642    0.4788632
46       0  1.00507737    0.4006814
47       0 -0.60304642   -0.8502264
48       0  0.77534540    0.8697719
49       0 -0.14358248    0.6743175
50       0  0.43074744    0.5179540
51       0  0.66047941    1.0261353
52       0 -1.75170627    0.5570449
53       0  0.31588146    0.5570449
54       0  1.00507737   -0.3811360
55       0  1.00507737    0.7915901
56       0  2.61320116   -0.6547721
attr(,"assign")
 [1] 0 1 2 3 4 4 4 4 5 6
attr(,"contrasts")
attr(,"contrasts")$fGRAZE
  2 3 4 5
1 0 0 0 0
2 1 0 0 0
3 0 1 0 0
4 0 0 1 0
5 0 0 0 1


$prior_only
[1] 0

attr(,"class")
[1] "standata" "list"    
loyn.brm3 |> stancode()
// generated with brms 2.22.0
functions {
}
data {
  int<lower=1> N;  // total number of observations
  vector[N] Y;  // response variable
  int<lower=1> K;  // number of population-level effects
  matrix[N, K] X;  // population-level design matrix
  int<lower=1> Kc;  // number of population-level effects after centering
  int prior_only;  // should the likelihood be ignored?
}
transformed data {
  matrix[N, Kc] Xc;  // centered version of X without an intercept
  vector[Kc] means_X;  // column means of X before centering
  for (i in 2:K) {
    means_X[i - 1] = mean(X[, i]);
    Xc[, i - 1] = X[, i] - means_X[i - 1];
  }
}
parameters {
  vector[Kc] b;  // regression coefficients
  real Intercept;  // temporary intercept for centered predictors
  real<lower=0> sigma;  // dispersion parameter
}
transformed parameters {
  real lprior = 0;  // prior contributions to the log posterior
  lprior += normal_lpdf(b | 0, 1.5);
  lprior += normal_lpdf(Intercept | 3.4, 0.1);
  lprior += student_t_lpdf(sigma | 3, 0, 1.5)
    - 1 * student_t_lccdf(0 | 3, 0, 1.5);
}
model {
  // likelihood including constants
  if (!prior_only) {
    // initialize linear predictor term
    vector[N] mu = rep_vector(0.0, N);
    mu += Intercept + Xc * b;
    mu = exp(mu);
    target += normal_lpdf(Y | mu, sigma);
  }
  // priors including constants
  target += lprior;
}
generated quantities {
  // actual population-level intercept
  real b_Intercept = Intercept - dot_product(means_X, b);
  // additionally sample draws from priors
  real prior_b = normal_rng(0,1.5);
  real prior_Intercept = normal_rng(3.4,0.1);
  real prior_sigma = student_t_rng(3,0,1.5);
  // use rejection sampling for truncated priors
  while (prior_sigma < 0) {
    prior_sigma = student_t_rng(3,0,1.5);
  }
}

6 MCMC sampling diagnostics

The bayesplot package offers a range of MCMC diagnostics as well as Posterior Probability Checks (PPC), all of which have a convenient plot() interface. Lets start with the MCMC diagnostics.

See list of available diagnostics by name
available_mcmc()
bayesplot MCMC module:
  mcmc_acf
  mcmc_acf_bar
  mcmc_areas
  mcmc_areas_ridges
  mcmc_combo
  mcmc_dens
  mcmc_dens_chains
  mcmc_dens_overlay
  mcmc_hex
  mcmc_hist
  mcmc_hist_by_chain
  mcmc_intervals
  mcmc_neff
  mcmc_neff_hist
  mcmc_nuts_acceptance
  mcmc_nuts_divergence
  mcmc_nuts_energy
  mcmc_nuts_stepsize
  mcmc_nuts_treedepth
  mcmc_pairs
  mcmc_parcoord
  mcmc_rank_ecdf
  mcmc_rank_hist
  mcmc_rank_overlay
  mcmc_recover_hist
  mcmc_recover_intervals
  mcmc_recover_scatter
  mcmc_rhat
  mcmc_rhat_hist
  mcmc_scatter
  mcmc_trace
  mcmc_trace_highlight
  mcmc_violin

Of these, we will focus on:

  • mcmc_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different shade of blue, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
plot(loyn.rstanarm3, plotfun = "mcmc_trace")

The chains appear well mixed and very similar

  • acf (auto-correlation function): plots the auto-correlation between successive MCMC sample lags for each parameter and each chain
plot(loyn.rstanarm3, "acf_bar")

There is no evidence of auto-correlation in the MCMC samples

  • Rhat: Rhat is a measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
plot(loyn.rstanarm3, "rhat_hist")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

All Rhat values are below 1.05, suggesting the chains have converged.

  • neff (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

plot(loyn.rstanarm3, "neff_hist")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Ratios all very high.

More diagnostics
plot(loyn.rstanarm3, "combo")

plot(loyn.rstanarm3, "violin")

The rstan package offers a range of MCMC diagnostics. Lets start with the MCMC diagnostics.

Of these, we will focus on:

  • stan_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
stan_trace(loyn.rstanarm3)

The chains appear well mixed and very similar

  • stan_acf (auto-correlation function): plots the auto-correlation between successive MCMC sample lags for each parameter and each chain
stan_ac(loyn.rstanarm3)

There is no evidence of auto-correlation in the MCMC samples

  • stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
stan_rhat(loyn.rstanarm3)
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

All Rhat values are below 1.05, suggesting the chains have converged.

  • stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

stan_ess(loyn.rstanarm3)
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Ratios all very high.

stan_dens(loyn.rstanarm3, separate_chains = TRUE)

The ggmean package also has a set of MCMC diagnostic functions. Lets start with the MCMC diagnostics.

Of these, we will focus on:

  • ggs_traceplot: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
loyn.ggs <- ggs(loyn.rstanarm3)
ggs_traceplot(loyn.ggs)

The chains appear well mixed and very similar

  • gss_autocorrelation (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
ggs_autocorrelation(loyn.ggs)

There is no evidence of autocorrelation in the MCMC samples

  • stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
ggs_Rhat(loyn.ggs)

All Rhat values are below 1.05, suggesting the chains have converged.

  • stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

ggs_effective(loyn.ggs)
Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
  always returns an ungrouped data frame and adjust accordingly.
ℹ The deprecated feature was likely used in the ggmcmc package.
  Please report the issue at <https://github.com/xfim/ggmcmc/issues/>.

Ratios all very high.

More diagnostics
ggs_crosscorrelation(loyn.ggs)

ggs_grb(loyn.ggs)

The bayesplot package offers a range of MCMC diagnostics as well as Posterior Probability Checks (PPC), all of which have a convenient plot() interface. Lets start with the MCMC diagnostics.

See list of available diagnostics by name
available_mcmc()
bayesplot MCMC module:
  mcmc_acf
  mcmc_acf_bar
  mcmc_areas
  mcmc_areas_ridges
  mcmc_combo
  mcmc_dens
  mcmc_dens_chains
  mcmc_dens_overlay
  mcmc_hex
  mcmc_hist
  mcmc_hist_by_chain
  mcmc_intervals
  mcmc_neff
  mcmc_neff_hist
  mcmc_nuts_acceptance
  mcmc_nuts_divergence
  mcmc_nuts_energy
  mcmc_nuts_stepsize
  mcmc_nuts_treedepth
  mcmc_pairs
  mcmc_parcoord
  mcmc_rank_ecdf
  mcmc_rank_hist
  mcmc_rank_overlay
  mcmc_recover_hist
  mcmc_recover_intervals
  mcmc_recover_scatter
  mcmc_rhat
  mcmc_rhat_hist
  mcmc_scatter
  mcmc_trace
  mcmc_trace_highlight
  mcmc_violin

Of these, we will focus on:

  • mcmc_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different shade of blue, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
loyn.brm3 |> mcmc_plot(type = "trace")
No divergences to plot.

The chains appear well mixed and very similar

  • acf (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
loyn.brm3 |> mcmc_plot(type = "acf_bar")

There is no evidence of autocorrelation in the MCMC samples

  • Rhat: Rhat is a measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
loyn.brm3 |> mcmc_plot(type = "rhat_hist")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

All Rhat values are below 1.05, suggesting the chains have converged.

  • neff (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

loyn.brm3 |> mcmc_plot(type = "neff_hist")
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Ratios all very high.

More diagnostics
loyn.brm3 |> mcmc_plot(type = "combo")

loyn.brm3 |> mcmc_plot(type = "violin")

The rstan package offers a range of MCMC diagnostics. Lets start with the MCMC diagnostics.

Of these, we will focus on:

  • stan_trace: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
loyn.brm3$fit |> stan_trace()
'pars' not specified. Showing first 10 parameters by default.

The chains appear well mixed and very similar

  • stan_acf (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
loyn.brm3$fit |> stan_ac()
'pars' not specified. Showing first 10 parameters by default.

There is no evidence of autocorrelation in the MCMC samples

  • stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
loyn.brm3$fit |> stan_rhat()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

All Rhat values are below 1.05, suggesting the chains have converged.

  • stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

loyn.brm3$fit |> stan_ess()
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Ratios all very high.

loyn.brm3$fit |> stan_dens(separate_chains = TRUE)
'pars' not specified. Showing first 10 parameters by default.

The ggmean package also has a set of MCMC diagnostic functions. Lets start with the MCMC diagnostics.

Of these, we will focus on:

  • ggs_traceplot: this plots the estimates of each parameter over the post-warmup length of each MCMC chain. Each chain is plotted in a different colour, with each parameter in its own facet. Ideally, each trace should just look like noise without any discernible drift and each of the traces for a specific parameter should look the same (i.e, should not be displaced above or below any other trace for that parameter).
loyn.ggs <- loyn.brm3 |> ggs(inc_warmup = FALSE, burnin = FALSE)
loyn.ggs |> ggs_traceplot()

The chains appear well mixed and very similar

  • gss_autocorrelation (autocorrelation function): plots the autocorrelation between successive MCMC sample lags for each parameter and each chain
loyn.ggs |> ggs_autocorrelation()

There is no evidence of autocorrelation in the MCMC samples

  • stan_rhat: Rhat is a scale reduction factor measure of convergence between the chains. The closer the values are to 1, the more the chains have converged. Values greater than 1.05 indicate a lack of convergence. There will be an Rhat value for each parameter estimated.
loyn.ggs |> ggs_Rhat()

All Rhat values are below 1.05, suggesting the chains have converged.

  • stan_ess (number of effective samples): the ratio of the number of effective samples (those not rejected by the sampler) to the number of samples provides an indication of the effectiveness (and efficiency) of the MCMC sampler. Ratios that are less than 0.5 for a parameter suggest that the sampler spent considerable time in difficult areas of the sampling domain and rejected more than half of the samples (replacing them with the previous effective sample).

    If the ratios are low, tightening the priors may help.

loyn.ggs |> ggs_effective()

Ratios all very high.

More diagnostics
loyn.ggs |> ggs_crosscorrelation()

loyn.ggs |> ggs_grb()

7 Model validation

Post predictive checks provide additional diagnostics about the fit of the model. Specifically, they provide a comparison between predictions drawn from the model and the observed data used to train the model.

See list of available diagnostics by name
available_ppc()
bayesplot PPC module:
  ppc_bars
  ppc_bars_grouped
  ppc_boxplot
  ppc_dens
  ppc_dens_overlay
  ppc_dens_overlay_grouped
  ppc_ecdf_overlay
  ppc_ecdf_overlay_grouped
  ppc_error_binned
  ppc_error_hist
  ppc_error_hist_grouped
  ppc_error_scatter
  ppc_error_scatter_avg
  ppc_error_scatter_avg_grouped
  ppc_error_scatter_avg_vs_x
  ppc_freqpoly
  ppc_freqpoly_grouped
  ppc_hist
  ppc_intervals
  ppc_intervals_grouped
  ppc_km_overlay
  ppc_km_overlay_grouped
  ppc_loo_intervals
  ppc_loo_pit_ecdf
  ppc_loo_pit_overlay
  ppc_loo_pit_qq
  ppc_loo_ribbon
  ppc_pit_ecdf
  ppc_pit_ecdf_grouped
  ppc_ribbon
  ppc_ribbon_grouped
  ppc_rootogram
  ppc_scatter
  ppc_scatter_avg
  ppc_scatter_avg_grouped
  ppc_stat
  ppc_stat_2d
  ppc_stat_freqpoly
  ppc_stat_freqpoly_grouped
  ppc_stat_grouped
  ppc_violin_grouped
  • dens_overlay: plots the density distribution of the observed data (black line) overlayed on top of 50 density distributions generated from draws from the model (light blue). Ideally, the 50 realisations should be roughly consistent with the observed data.
pp_check(loyn.rstanarm3, plotfun = "dens_overlay")

The model draws appear deviate from the observed data.

  • error_scatter_avg: this plots the observed values against the average residuals. Similar to a residual plot, we do not want to see any patterns in this plot. There is some pattern remaining in these residuals.
pp_check(loyn.rstanarm3, plotfun = "error_scatter_avg")

The predictive error seems to be related to the predictor - the model performs poorest at higher mussel clump areas.

  • error_scatter_avg_vs_x: this is similar to a regular residual plot and as such should be interpreted as such. Again, this is not interpretable for binary data.
pp_check(loyn.rstanarm3, x = loyn$AREA, plotfun = "error_scatter_avg_vs_x")

  • intervals: plots the observed data overlayed on top of posterior predictions associated with each level of the predictor. Ideally, the observed data should all fall within the predictive intervals.
pp_check(loyn.rstanarm3, x = loyn$AREA, plotfun = "intervals")

The modelled predictions seem to underestimate the uncertainty with increasing mussel clump area.

  • ribbon: this is just an alternative way of expressing the above plot.
pp_check(loyn.rstanarm3, x = loyn$AREA, plotfun = "ribbon")

The shinystan package allows the full suite of MCMC diagnostics and posterior predictive checks to be accessed via a web interface.

# library(shinystan)
# launch_shinystan(loyn.rstanarm3)

DHARMa residuals provide very useful diagnostics. Unfortunately, we cannot directly use the simulateResiduals() function to generate the simulated residuals. However, if we are willing to calculate some of the components yourself, we can still obtain the simulated residuals from the fitted stan model.

We need to supply:

  • simulated (predicted) responses associated with each observation.
  • observed values
  • fitted (predicted) responses (averaged) associated with each observation
preds <- posterior_predict(loyn.rstanarm3, ndraws = 250, summary = FALSE)
loyn.resids <- createDHARMa(
  simulatedResponse = t(preds),
  observedResponse = loyn$ABUND,
  fittedPredictedResponse = apply(preds, 2, median),
  integerResponse = FALSE
)
plot(loyn.resids)

Conclusions:

  • the simulated residuals suggest a general lack of fit due to over dispersion and outliers
  • perhaps we should explore a negative binomial model

Post predictive checks provide additional diagnostics about the fit of the model. Specifically, they provide a comparison between predictions drawn from the model and the observed data used to train the model.

See list of available diagnostics by name
available_ppc()
bayesplot PPC module:
  ppc_bars
  ppc_bars_grouped
  ppc_boxplot
  ppc_dens
  ppc_dens_overlay
  ppc_dens_overlay_grouped
  ppc_ecdf_overlay
  ppc_ecdf_overlay_grouped
  ppc_error_binned
  ppc_error_hist
  ppc_error_hist_grouped
  ppc_error_scatter
  ppc_error_scatter_avg
  ppc_error_scatter_avg_grouped
  ppc_error_scatter_avg_vs_x
  ppc_freqpoly
  ppc_freqpoly_grouped
  ppc_hist
  ppc_intervals
  ppc_intervals_grouped
  ppc_km_overlay
  ppc_km_overlay_grouped
  ppc_loo_intervals
  ppc_loo_pit_ecdf
  ppc_loo_pit_overlay
  ppc_loo_pit_qq
  ppc_loo_ribbon
  ppc_pit_ecdf
  ppc_pit_ecdf_grouped
  ppc_ribbon
  ppc_ribbon_grouped
  ppc_rootogram
  ppc_scatter
  ppc_scatter_avg
  ppc_scatter_avg_grouped
  ppc_stat
  ppc_stat_2d
  ppc_stat_freqpoly
  ppc_stat_freqpoly_grouped
  ppc_stat_grouped
  ppc_violin_grouped
  • dens_overlay: plots the density distribution of the observed data (black line) overlayed on top of 50 density distributions generated from draws from the model (light blue). Ideally, the 50 realisations should be roughly consistent with the observed data.
loyn.brm3 |> pp_check(type = "dens_overlay", ndraws = 100)

The model draws appear deviate from the observed data.

  • error_scatter_avg: this plots the observed values against the average residuals. Similar to a residual plot, we do not want to see any patterns in this plot. There is some pattern remaining in these residuals.
loyn.brm3 |> pp_check(type = "error_scatter_avg")
Using all posterior draws for ppc type 'error_scatter_avg' by default.

The predictive error seems to be related to the predictor - the model performs poorest at higher mussel clump areas.

  • error_scatter_avg_vs_x: this is similar to a regular residual plot and as such should be interpreted as such. Again, this is not interpretable for binary data.
loyn.brm3 |> pp_check(x = "AREA", type = "error_scatter_avg_vs_x")
Using all posterior draws for ppc type 'error_scatter_avg_vs_x' by default.

  • intervals: plots the observed data overlayed on top of posterior predictions associated with each level of the predictor. Ideally, the observed data should all fall within the predictive intervals.
loyn.brm3 |> pp_check(x = "AREA", type = "intervals")
Using all posterior draws for ppc type 'intervals' by default.

The modelled predictions seem to underestimate the uncertainty with increasing mussel clump area.

  • ribbon: this is just an alternative way of expressing the above plot.
loyn.brm3 |> pp_check(x = "AREA", type = "ribbon")
Using all posterior draws for ppc type 'ribbon' by default.

The shinystan package allows the full suite of MCMC diagnostics and posterior predictive checks to be accessed via a web interface.

# library(shinystan)
# launch_shinystan(loyn.brm3)

DHARMa residuals provide very useful diagnostics. Unfortunately, we cannot directly use the simulateResiduals() function to generate the simulated residuals. However, if we are willing to calculate some of the components yourself, we can still obtain the simulated residuals from the fitted stan model.

We need to supply:

  • simulated (predicted) responses associated with each observation.
  • observed values
  • fitted (predicted) responses (averaged) associated with each observation
preds <- loyn.brm3 |> posterior_predict(ndraws = 250, summary = FALSE)
loyn.resids <- createDHARMa(
  simulatedResponse = t(preds),
  observedResponse = loyn$ABUND,
  fittedPredictedResponse = apply(preds, 2, median),
  integerResponse = FALSE
)
loyn.resids |> plot()

loyn.resids <- make_brms_dharma_res(loyn.brm3, integerResponse = FALSE)
wrap_elements(~ testUniformity(loyn.resids)) +
  wrap_elements(~ plotResiduals(loyn.resids, form = factor(rep(1, nrow(loyn))))) +
  wrap_elements(~ plotResiduals(loyn.resids, quantreg = TRUE)) +
  wrap_elements(~ testDispersion(loyn.resids))

Conclusions:

  • the simulated residuals suggest a general lack of fit due to over-dispersion and outliers
  • perhaps we should explore a negative binomial model

8 Partial effects plots

loyn.rstanarm3 |>
  ggpredict() |>
  plot(show_data = TRUE, facet = TRUE)
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.

# loyn.rstanarm3 |> ggemmeans(~AREA,  type='fixed') |> plot(show_data=TRUE) + scale_y_log10()
loyn.rstanarm3 |>
  fitted_draws(newdata = loyn) |>
  median_hdci() |>
  ggplot(aes(x = AREA, y = .value)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), fill = "blue", alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10()
Warning: `fitted_draws` and `add_fitted_draws` are deprecated as their names were confusing.
- Use [add_]epred_draws() to get the expectation of the posterior predictive.
- Use [add_]linpred_draws() to get the distribution of the linear predictor.
- For example, you used [add_]fitted_draws(..., scale = "response"), which
  means you most likely want [add_]epred_draws(...).
NOTE: When updating to the new functions, note that the `model` parameter is now
  named `object` and the `n` parameter is now named `ndraws`.

loyn.brm3 |>
  conditional_effects() |>
  plot(ask = FALSE, points = TRUE, plot = FALSE) |>
  wrap_plots()

loyn.brm3 |>
  conditional_effects() |>
  plot(ask = FALSE, points = TRUE, plot = FALSE) |>
  wrap_plots() &
  scale_y_log10()

g <- loyn.brm3 |>
  conditional_effects() |>
  plot(ask = FALSE, points = TRUE, plot = FALSE)
library(patchwork)
length(g)
[1] 6
(g[[1]] + scale_x_log10()) +
  (g[[2]] + scale_x_log10()) +
  (g[[3]] + scale_x_log10()) +
  g[[4]] +
  g[[5]] +
  g[[6]]

loyn.brm3 |>
  ggpredict() |>
  plot(show_data = TRUE) |>
  wrap_plots()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.

loyn.brm3 |>
  ggpredict() |>
  plot(show_data = TRUE) |>
  wrap_plots() &
  scale_y_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.
Warning in scale_y_log10(): log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.
log-10 transformation introduced infinite values.

This will be slow …

loyn.brm3 |>
  ggemmeans("AREA[0:1000]") |>
  plot(show_data = TRUE) |>
  wrap_plots() &
  scale_y_log10() &
  scale_x_log10()
Data points may overlap. Use the `jitter` argument to add some amount of
  random variation to the location of data points and avoid overplotting.
Scale for y is already present.
Adding another scale for y, which will replace the existing scale.

loyn.brm3 |>
  epred_draws(newdata = loyn) |>
  median_hdci(.epred) |>
  ggplot(aes(x = AREA, y = .epred, colour = fGRAZE, fill = fGRAZE)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), colour = NA, alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10() +
  scale_x_log10()

9 Model investigation

The summary() method generates simple summaries (mean, standard deviation as well as 10, 50 and 90 percentiles).

summary(loyn.rstanarm3)

Model Info:
 function:     stan_glm
 family:       gaussian [log]
 formula:      ABUND ~ scale(log(DIST)) + scale(log(LDIST)) + scale(log(AREA)) + 
       fGRAZE + scale(ALT) + scale(YR.ISOL)
 algorithm:    sampling
 sample:       1500 (posterior sample size)
 priors:       see help('prior_summary')
 observations: 56
 predictors:   10

Estimates:
                    mean   sd   10%   50%   90%
(Intercept)        3.0    0.1  2.9   3.0   3.2 
scale(log(DIST))   0.0    0.1 -0.1   0.0   0.1 
scale(log(LDIST))  0.1    0.1  0.0   0.1   0.1 
scale(log(AREA))   0.2    0.1  0.1   0.2   0.3 
fGRAZE2            0.0    0.2 -0.2   0.0   0.2 
fGRAZE3            0.0    0.1 -0.1   0.0   0.2 
fGRAZE4            0.0    0.2 -0.2   0.0   0.2 
fGRAZE5           -1.1    0.4 -1.5  -1.0  -0.6 
scale(ALT)         0.0    0.1 -0.1   0.0   0.1 
scale(YR.ISOL)     0.0    0.1 -0.1   0.0   0.1 
sigma              6.6    0.7  5.7   6.5   7.5 

Fit Diagnostics:
           mean   sd   10%   50%   90%
mean_PPD 19.3    1.3 17.7  19.4  20.9 

The mean_ppd is the sample average posterior predictive distribution of the outcome variable (for details see help('summary.stanreg')).

MCMC diagnostics
                  mcse Rhat n_eff
(Intercept)       0.0  1.0  1355 
scale(log(DIST))  0.0  1.0  1387 
scale(log(LDIST)) 0.0  1.0  1444 
scale(log(AREA))  0.0  1.0  1285 
fGRAZE2           0.0  1.0  1557 
fGRAZE3           0.0  1.0  1524 
fGRAZE4           0.0  1.0  1641 
fGRAZE5           0.0  1.0  1464 
scale(ALT)        0.0  1.0  1421 
scale(YR.ISOL)    0.0  1.0  1492 
sigma             0.0  1.0  1301 
mean_PPD          0.0  1.0  1492 
log-posterior     0.1  1.0  1408 

For each parameter, mcse is Monte Carlo standard error, n_eff is a crude measure of effective sample size, and Rhat is the potential scale reduction factor on split chains (at convergence Rhat=1).

Conclusions:

  • in the Model Info, we are informed that the total MCMC posterior sample size is 1500 and that there were 56 raw observations.
  • the estimated intercept (expected bird abundance when grazing intensity is equal to 1 and all of the continuous predictors are at their respective averages) is 3.04. This is the mean of the posterior distribution for this parameter. If we back-transform this to the response scale, this becomes 20.91.
  • the estimated slope associated with DIST (rate at which the abundance of birds changes per 1 unit change in log-transformed Distance to nearest patch holding other continuous predictors constant and grazing intensity at level 1), is -0.01 (mean) or -0.09 (median) with a standard deviation of 0. The 90% credibility intervals indicate that we are 90% confident that the slope is between -0.01 and -0.01 - e.g. there is no evidence of a significant trend.
  • associated with each of the continuous predictors is a partial slope. Each partial slope is the rate of change between bird abundance and the associated predictor (on the log scale due to the link and based on 1 unit change in the predictor on the scale of the predictor). For example, for every one unit change in centred log patch Area, bird abundance is expected to increase by (log) 0.21.
  • if we back transform this slope (by exponentiation), we get a partial slope for centred log Area of 1.24. This is interpreted as - for every 1 unit increase in (scaled log) Area, the bird abundance is expected to increase 1.24 fold. That is, there is a 23.84 percent increase per 1 unit increase in centred log Area.
  • Rhat and number of effective samples for each parameter are also provided as MCMC diagnostics and all look good.
loyn.rstanarm3$stanfit |>
  summarise_draws(
    median,
    HDInterval::hdi,
    rhat, length, ess_bulk, ess_tail
  )
# A tibble: 13 × 8
   variable              median    lower    upper  rhat length ess_bulk ess_tail
   <chr>                  <dbl>    <dbl>    <dbl> <dbl>  <dbl>    <dbl>    <dbl>
 1 (Intercept)          3.04e+0  2.80e+0  3.25e+0 0.999   1500    1379.    1426.
 2 scale(log(DIST))    -1.11e-2 -1.26e-1  1.07e-1 1.000   1500    1401.    1350.
 3 scale(log(LDIST))    5.27e-2 -6.01e-2  1.88e-1 1.00    1500    1454.    1523.
 4 scale(log(AREA))     2.12e-1  9.31e-2  3.33e-1 1.00    1500    1321.    1453.
 5 fGRAZE2              4.16e-2 -2.77e-1  3.27e-1 0.999   1500    1543.    1500.
 6 fGRAZE3              4.11e-2 -2.44e-1  2.92e-1 1.000   1500    1495.    1378.
 7 fGRAZE4              1.12e-2 -3.29e-1  3.12e-1 1.000   1500    1651.    1457.
 8 fGRAZE5             -1.04e+0 -1.77e+0 -3.89e-1 1.00    1500    1480.    1420.
 9 scale(ALT)          -3.59e-3 -1.07e-1  9.68e-2 0.999   1500    1367.    1462.
10 scale(YR.ISOL)      -1.11e-4 -1.31e-1  1.85e-1 1.00    1500    1499.    1422.
11 sigma                6.51e+0  5.26e+0  7.91e+0 1.00    1500    1292.    1501.
12 mean_PPD             1.94e+1  1.68e+1  2.17e+1 0.999   1500    1511.    1458.
13 log-posterior       -1.96e+2 -2.02e+2 -1.92e+2 1.00    1500    1439.    1512.

We can also alter the CI level.

loyn.rstanarm3$stanfit |>
  summarise_draws(
    median,
    ~ HDInterval::hdi(.x, credMass = 0.9),
    rhat, length, ess_bulk, ess_tail
  )
# A tibble: 13 × 8
   variable              median    lower    upper  rhat length ess_bulk ess_tail
   <chr>                  <dbl>    <dbl>    <dbl> <dbl>  <dbl>    <dbl>    <dbl>
 1 (Intercept)          3.04e+0  2.85e+0  3.22e+0 0.999   1500    1379.    1426.
 2 scale(log(DIST))    -1.11e-2 -1.05e-1  8.85e-2 1.000   1500    1401.    1350.
 3 scale(log(LDIST))    5.27e-2 -4.82e-2  1.53e-1 1.00    1500    1454.    1523.
 4 scale(log(AREA))     2.12e-1  1.13e-1  3.13e-1 1.00    1500    1321.    1453.
 5 fGRAZE2              4.16e-2 -2.02e-1  2.98e-1 0.999   1500    1543.    1500.
 6 fGRAZE3              4.11e-2 -1.79e-1  2.71e-1 1.000   1500    1495.    1378.
 7 fGRAZE4              1.12e-2 -2.68e-1  2.77e-1 1.000   1500    1651.    1457.
 8 fGRAZE5             -1.04e+0 -1.66e+0 -4.95e-1 1.00    1500    1480.    1420.
 9 scale(ALT)          -3.59e-3 -7.93e-2  8.79e-2 0.999   1500    1367.    1462.
10 scale(YR.ISOL)      -1.11e-4 -1.22e-1  1.35e-1 1.00    1500    1499.    1422.
11 sigma                6.51e+0  5.49e+0  7.70e+0 1.00    1500    1292.    1501.
12 mean_PPD             1.94e+1  1.73e+1  2.14e+1 0.999   1500    1511.    1458.
13 log-posterior       -1.96e+2 -2.00e+2 -1.92e+2 1.00    1500    1439.    1512.

And on a ratio scale

loyn.rstanarm3$stanfit |>
  summarise_draws(
    ~ median(exp(.x)),
    ~ HDInterval::hdi(exp(.x)),
    rhat, length, ess_bulk, ess_tail
  )
# A tibble: 13 × 8
   variable  `~median(exp(.x))`    lower    upper  rhat length ess_bulk ess_tail
   <chr>                  <dbl>    <dbl>    <dbl> <dbl>  <dbl>    <dbl>    <dbl>
 1 (Interce…          2.10 e+ 1 1.63e+ 1 2.56e+ 1 0.999   1500    1379.    1426.
 2 scale(lo…          9.89 e- 1 8.82e- 1 1.11e+ 0 1.000   1500    1401.    1350.
 3 scale(lo…          1.05 e+ 0 9.42e- 1 1.21e+ 0 1.00    1500    1454.    1523.
 4 scale(lo…          1.24 e+ 0 1.08e+ 0 1.38e+ 0 1.00    1500    1321.    1453.
 5 fGRAZE2            1.04 e+ 0 7.29e- 1 1.35e+ 0 0.999   1500    1543.    1500.
 6 fGRAZE3            1.04 e+ 0 7.78e- 1 1.33e+ 0 1.000   1500    1495.    1378.
 7 fGRAZE4            1.01 e+ 0 7.20e- 1 1.37e+ 0 1.000   1500    1651.    1457.
 8 fGRAZE5            3.52 e- 1 1.46e- 1 6.31e- 1 1.00    1500    1480.    1420.
 9 scale(AL…          9.96 e- 1 8.99e- 1 1.10e+ 0 0.999   1500    1367.    1462.
10 scale(YR…          1.000e+ 0 8.51e- 1 1.17e+ 0 1.00    1500    1499.    1422.
11 sigma              6.74 e+ 2 1.19e+ 2 2.38e+ 3 1.00    1500    1292.    1501.
12 mean_PPD           2.60 e+ 8 4.18e+ 6 1.93e+ 9 0.999   1500    1511.    1458.
13 log-post…          5.90 e-86 5.23e-91 1.65e-84 1.00    1500    1439.    1512.
loyn.rstanarm3 |> tidy_draws()
# A tibble: 1,500 × 20
   .chain .iteration .draw `(Intercept)` `scale(log(DIST))` `scale(log(LDIST))`
    <int>      <int> <int>         <dbl>              <dbl>               <dbl>
 1      1          1     1          3.14            0.0435               0.0764
 2      1          2     2          3.11            0.0569               0.0394
 3      1          3     3          2.94           -0.00675              0.0242
 4      1          4     4          2.84           -0.0132              -0.0329
 5      1          5     5          3.23           -0.00739              0.0778
 6      1          6     6          3.15            0.0123               0.0689
 7      1          7     7          2.84            0.0452              -0.108 
 8      1          8     8          2.95            0.00570              0.0458
 9      1          9     9          3.03           -0.0221               0.0551
10      1         10    10          3.19           -0.165                0.103 
# ℹ 1,490 more rows
# ℹ 14 more variables: `scale(log(AREA))` <dbl>, fGRAZE2 <dbl>, fGRAZE3 <dbl>,
#   fGRAZE4 <dbl>, fGRAZE5 <dbl>, `scale(ALT)` <dbl>, `scale(YR.ISOL)` <dbl>,
#   sigma <dbl>, accept_stat__ <dbl>, stepsize__ <dbl>, treedepth__ <dbl>,
#   n_leapfrog__ <dbl>, divergent__ <dbl>, energy__ <dbl>
loyn.rstanarm3$stanfit |> as_draws_df()
# A draws_df: 500 iterations, 3 chains, and 13 variables
   (Intercept) scale(log(DIST)) scale(log(LDIST)) scale(log(AREA)) fGRAZE2
1          3.1           0.0435             0.076             0.12   0.043
2          3.1           0.0569             0.039             0.19  -0.118
3          2.9          -0.0068             0.024             0.27   0.273
4          2.8          -0.0132            -0.033             0.41   0.298
5          3.2          -0.0074             0.078             0.13  -0.235
6          3.2           0.0123             0.069             0.17  -0.064
7          2.8           0.0452            -0.108             0.31   0.238
8          3.0           0.0057             0.046             0.22   0.099
9          3.0          -0.0221             0.055             0.27   0.217
10         3.2          -0.1653             0.103             0.15  -0.419
   fGRAZE3 fGRAZE4 fGRAZE5
1    0.101  -0.194   -1.76
2    0.046   0.159   -1.82
3    0.113  -0.014   -0.96
4    0.161   0.413   -0.49
5   -0.101  -0.102   -1.55
6   -0.134  -0.093   -1.04
7    0.132   0.094   -0.67
8    0.190   0.023   -1.49
9    0.145   0.024   -0.76
10  -0.240  -0.193   -1.00
# ... with 1490 more draws, and 5 more variables
# ... hidden reserved variables {'.chain', '.iteration', '.draw'}
loyn.rstanarm3$stanfit |>
  as_draws_df() |>
  summarise_draws(
    "median",
    ~ HDInterval::hdi(.x),
    "rhat",
    "ess_bulk"
  )
# A tibble: 13 × 6
   variable               median     lower     upper  rhat ess_bulk
   <chr>                   <dbl>     <dbl>     <dbl> <dbl>    <dbl>
 1 (Intercept)          3.04        2.80      3.25   0.999    1379.
 2 scale(log(DIST))    -0.0111     -0.126     0.107  1.000    1401.
 3 scale(log(LDIST))    0.0527     -0.0601    0.188  1.00     1454.
 4 scale(log(AREA))     0.212       0.0931    0.333  1.00     1321.
 5 fGRAZE2              0.0416     -0.277     0.327  0.999    1543.
 6 fGRAZE3              0.0411     -0.244     0.292  1.000    1495.
 7 fGRAZE4              0.0112     -0.329     0.312  1.000    1651.
 8 fGRAZE5             -1.04       -1.77     -0.389  1.00     1480.
 9 scale(ALT)          -0.00359    -0.107     0.0968 0.999    1367.
10 scale(YR.ISOL)      -0.000111   -0.131     0.185  1.00     1499.
11 sigma                6.51        5.26      7.91   1.00     1292.
12 mean_PPD            19.4        16.8      21.7    0.999    1511.
13 log-posterior     -196.       -202.     -192.     1.00     1439.
tidyMCMC(loyn.rstanarm3$stanfit,
  estimate.method = "median", conf.int = TRUE,
  conf.method = "HPDinterval", rhat = TRUE, ess = TRUE
)
# A tibble: 13 × 7
   term                 estimate std.error  conf.low conf.high  rhat   ess
   <chr>                   <dbl>     <dbl>     <dbl>     <dbl> <dbl> <int>
 1 (Intercept)          3.04        0.116     2.80      3.25   0.999  1355
 2 scale(log(DIST))    -0.0111      0.0594   -0.126     0.107  0.999  1387
 3 scale(log(LDIST))    0.0526      0.0626   -0.0601    0.188  0.999  1444
 4 scale(log(AREA))     0.214       0.0614    0.0931    0.333  0.999  1285
 5 fGRAZE2              0.0400      0.157    -0.277     0.327  0.999  1557
 6 fGRAZE3              0.0431      0.138    -0.244     0.292  0.998  1524
 7 fGRAZE4              0.00268     0.167    -0.329     0.312  0.999  1641
 8 fGRAZE5             -1.06        0.353    -1.77     -0.389  1.00   1464
 9 scale(ALT)          -0.000756    0.0524   -0.107     0.0968 0.999  1421
10 scale(YR.ISOL)       0.00666     0.0789   -0.131     0.185  1.00   1492
11 sigma                6.56        0.685     5.26      7.91   1.00   1301
12 mean_PPD            19.3         1.25     16.8      21.7    0.999  1492
13 log-posterior     -197.          2.61   -202.     -192.     1.000  1408

Conclusions:

  • the estimated intercept (expected bird abundance when grazing intensity is equal to 1 and all of the continuous predictors are at their respective averages) is NA. This is the mean of the posterior distribution for this parameter. If we back-transform this to the response scale, this becomes NA.
  • the estimated slope associated with DIST (rate at which the abundance of birds changes per 1 unit change in log-transformed Distance to nearest patch holding other continuous predictors constant and grazing intensity at level 1), is NA (mean) or -0.13 with a standard deviation of -0.01. The 90% credibility intervals indicate that we are 90% confident that the slope is between NA and 0.11 - e.g. there is no evidence of a significant trend.
  • associated with each of the continuous predictors is a partial slope. Each partial slope is the rate of change between bird abundance and the associated predictor (on the log scale due to the link and based on 1 unit change in the predictor on the scale of the predictor). For example, for every one unit change in centred log patch Area, bird abundance is expected to increase by (log) NA.
  • if we back transform this slope (by exponentiation), we get a partial slope for centred log Area of NA. This is interpreted as - for every 1 unit increase in (scaled log) Area, the bird abundance is expected to increase NA fold. That is, there is a NA percent increase per 1 unit increase in centred log Area.
  • Rhat and number of effective samples for each parameter are also provided as MCMC diagnostics and all look good.
loyn.rstanarm3 |> get_variables()
 [1] "(Intercept)"       "scale(log(DIST))"  "scale(log(LDIST))"
 [4] "scale(log(AREA))"  "fGRAZE2"           "fGRAZE3"          
 [7] "fGRAZE4"           "fGRAZE5"           "scale(ALT)"       
[10] "scale(YR.ISOL)"    "sigma"             "accept_stat__"    
[13] "stepsize__"        "treedepth__"       "n_leapfrog__"     
[16] "divergent__"       "energy__"         
loyn.draw <- loyn.rstanarm3 |> gather_draws(`.Intercept.*|.*AREA.*|.*DIST.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE)
loyn.draw
# A tibble: 15,000 × 5
# Groups:   .variable [10]
   .chain .iteration .draw .variable   .value
    <int>      <int> <int> <chr>        <dbl>
 1      1          1     1 (Intercept)   3.14
 2      1          2     2 (Intercept)   3.11
 3      1          3     3 (Intercept)   2.94
 4      1          4     4 (Intercept)   2.84
 5      1          5     5 (Intercept)   3.23
 6      1          6     6 (Intercept)   3.15
 7      1          7     7 (Intercept)   2.84
 8      1          8     8 (Intercept)   2.95
 9      1          9     9 (Intercept)   3.03
10      1         10    10 (Intercept)   3.19
# ℹ 14,990 more rows
loyn.rstanarm3 |> plot(plotfun = "mcmc_intervals")

loyn.rstanarm3 |>
  gather_draws(`.*AREA.*|.*DIST.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE) |>
  ggplot() +
  stat_halfeye(aes(x = .value, y = .variable)) +
  facet_wrap(~.variable, scales = "free")

loyn.rstanarm3 |>
  gather_draws(`.*AREA.*|.*DIST.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE) |>
  ggplot() +
  stat_halfeye(aes(x = .value, y = .variable)) +
  geom_vline(xintercept = 1, linetype = "dashed")

loyn.rstanarm3 |>
  gather_draws(`.*AREA.*|.*DIST.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE) |>
  ggplot() +
  geom_density_ridges(aes(x = .value, y = .variable), alpha = 0.4) +
  geom_vline(xintercept = 0, linetype = "dashed")
Picking joint bandwidth of 0.0254

## Or on a fractional scale
loyn.rstanarm3 |>
  gather_draws(`.*AREA.*|.*DIST.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE) |>
  ggplot() +
  geom_density_ridges_gradient(
    aes(
      x = exp(.value),
      y = .variable,
      fill = stat(x)
    ),
    alpha = 0.4, colour = "white",
    quantile_lines = TRUE,
    quantiles = c(0.025, 0.975)
  ) +
  geom_vline(xintercept = 1, linetype = "dashed") +
  scale_x_continuous(trans = scales::log2_trans()) +
  scale_fill_viridis_c(option = "C")
Warning: `stat(x)` was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(x)` instead.
Picking joint bandwidth of 0.0366

loyn.rstanarm3 |> spread_draws(`.Intercept.*|.*DIST.*|.*AREA.*|.*GRAZE.*|.*ALT.*|.*YR.*`, regex = TRUE)
# A tibble: 1,500 × 13
   .chain .iteration .draw `(Intercept)` `scale(log(DIST))` `scale(log(LDIST))`
    <int>      <int> <int>         <dbl>              <dbl>               <dbl>
 1      1          1     1          3.14            0.0435               0.0764
 2      1          2     2          3.11            0.0569               0.0394
 3      1          3     3          2.94           -0.00675              0.0242
 4      1          4     4          2.84           -0.0132              -0.0329
 5      1          5     5          3.23           -0.00739              0.0778
 6      1          6     6          3.15            0.0123               0.0689
 7      1          7     7          2.84            0.0452              -0.108 
 8      1          8     8          2.95            0.00570              0.0458
 9      1          9     9          3.03           -0.0221               0.0551
10      1         10    10          3.19           -0.165                0.103 
# ℹ 1,490 more rows
# ℹ 7 more variables: `scale(log(AREA))` <dbl>, fGRAZE2 <dbl>, fGRAZE3 <dbl>,
#   fGRAZE4 <dbl>, fGRAZE5 <dbl>, `scale(ALT)` <dbl>, `scale(YR.ISOL)` <dbl>
loyn.rstanarm3 |>
  posterior_samples() |>
  as.tibble()
Warning: `as.tibble()` was deprecated in tibble 2.0.0.
ℹ Please use `as_tibble()` instead.
ℹ The signature and semantics have changed, see `?as_tibble`.
Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
recommended alternatives.
# A tibble: 1,500 × 11
   `(Intercept)` `scale(log(DIST))` `scale(log(LDIST))` `scale(log(AREA))`
           <dbl>              <dbl>               <dbl>              <dbl>
 1          3.14            0.0435               0.0764              0.116
 2          3.11            0.0569               0.0394              0.187
 3          2.94           -0.00675              0.0242              0.271
 4          2.84           -0.0132              -0.0329              0.414
 5          3.23           -0.00739              0.0778              0.133
 6          3.15            0.0123               0.0689              0.174
 7          2.84            0.0452              -0.108               0.307
 8          2.95            0.00570              0.0458              0.216
 9          3.03           -0.0221               0.0551              0.267
10          3.19           -0.165                0.103               0.152
# ℹ 1,490 more rows
# ℹ 7 more variables: fGRAZE2 <dbl>, fGRAZE3 <dbl>, fGRAZE4 <dbl>,
#   fGRAZE5 <dbl>, `scale(ALT)` <dbl>, `scale(YR.ISOL)` <dbl>, sigma <dbl>
loyn.rstanarm3 |>
  bayes_R2() |>
  median_hdci()
          y      ymin      ymax .width .point .interval
1 0.6486707 0.5012478 0.7418391   0.95 median      hdci

The summary() method generates simple summaries (mean, standard deviation as well as 10, 50 and 90 percentiles).

loyn.brm3 |> summary()
 Family: gaussian 
  Links: mu = log; sigma = identity 
Formula: ABUND ~ scale(log(DIST)) + scale(log(LDIST)) + scale(log(AREA)) + fGRAZE + scale(ALT) + scale(YR.ISOL) 
   Data: loyn (Number of observations: 56) 
  Draws: 3 chains, each with iter = 5000; warmup = 2500; thin = 5;
         total post-warmup draws = 1500

Regression Coefficients:
              Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
Intercept         3.13      0.11     2.92     3.34 1.00     1494     1387
scalelogDIST     -0.01      0.05    -0.11     0.10 1.00     1358     1419
scalelogLDIST     0.04      0.06    -0.08     0.16 1.00     1620     1588
scalelogAREA      0.19      0.05     0.08     0.30 1.00     1504     1420
fGRAZE2           0.03      0.15    -0.26     0.30 1.00     1389     1364
fGRAZE3           0.02      0.13    -0.24     0.27 1.00     1479     1499
fGRAZE4          -0.01      0.15    -0.32     0.29 1.00     1481     1414
fGRAZE5          -0.76      0.26    -1.26    -0.24 1.01     1621     1454
scaleALT          0.00      0.05    -0.09     0.10 1.00     1595     1459
scaleYR.ISOL     -0.00      0.07    -0.14     0.14 1.00     1530     1501

Further Distributional Parameters:
      Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
sigma     6.68      0.73     5.46     8.38 1.00     1469     1381

Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
and Tail_ESS are effective sample size measures, and Rhat is the potential
scale reduction factor on split chains (at convergence, Rhat = 1).

Conclusions:

  • in the Model Info, we are informed that the total MCMC posterior sample size is 1500 and that there were 56 raw observations.
  • the estimated intercept (expected bird abundance when grazing intensity is equal to 1 and all of the continuous predictors are at their respective averages) is 3.13. This is the mean of the posterior distribution for this parameter. If we back-transform this to the response scale, this becomes 22.95.
  • the estimated slope associated with DIST (rate at which the abundance of birds changes per 1 unit change in log-transformed Distance to nearest patch holding other continuous predictors constant and grazing intensity at level 1), is -0.01 (mean) or 0.1 (median) with a standard deviation of 0.05. The 90% credibility intervals indicate that we are 90% confident that the slope is between -0.01 and 1 - e.g. there is no evidence of a significant trend.
  • associated with each of the continuous predictors is a partial slope. Each partial slope is the rate of change between bird abundance and the associated predictor (on the log scale due to the link and based on 1 unit change in the predictor on the scale of the predictor). For example, for every one unit change in centred log patch Area, bird abundance is expected to increase by (log) 0.19.
  • if we back transform this slope (by exponentiation), we get a partial slope for centred log Area of 1.21. This is interpreted as - for every 1 unit increase in (scaled log) Area, the bird abundance is expected to increase 1.21 fold. That is, there is a 20.8 percent increase per 1 unit increase in centred log Area.
  • Rhat and number of effective samples for each parameter are also provided as MCMC diagnostics and all look good.
loyn.brm3 |> tidy_draws()
# A tibble: 1,500 × 26
   .chain .iteration .draw b_Intercept b_scalelogDIST b_scalelogLDIST
    <int>      <int> <int>       <dbl>          <dbl>           <dbl>
 1      1          1     1        3.06      -0.0226           0.115  
 2      1          2     2        3.15      -0.0107           0.0273 
 3      1          3     3        3.08       0.0265           0.0438 
 4      1          4     4        3.17       0.00838         -0.00298
 5      1          5     5        3.11      -0.0221           0.0630 
 6      1          6     6        2.90      -0.0547           0.0807 
 7      1          7     7        3.15      -0.0633           0.131  
 8      1          8     8        3.20      -0.0620           0.0365 
 9      1          9     9        3.22       0.0353           0.0591 
10      1         10    10        2.99      -0.000956        -0.0117 
# ℹ 1,490 more rows
# ℹ 20 more variables: b_scalelogAREA <dbl>, b_fGRAZE2 <dbl>, b_fGRAZE3 <dbl>,
#   b_fGRAZE4 <dbl>, b_fGRAZE5 <dbl>, b_scaleALT <dbl>, b_scaleYR.ISOL <dbl>,
#   sigma <dbl>, Intercept <dbl>, prior_Intercept <dbl>, prior_b <dbl>,
#   prior_sigma <dbl>, lprior <dbl>, lp__ <dbl>, accept_stat__ <dbl>,
#   stepsize__ <dbl>, treedepth__ <dbl>, n_leapfrog__ <dbl>, divergent__ <dbl>,
#   energy__ <dbl>
loyn.brm3 |>
  tidy_draws() |>
  dplyr::select(starts_with("b_")) |>
  exp() |>
  summarise_draws(
    median,
    HDInterval::hdi,
    rhat,
    length,
    ess_bulk, ess_tail
  )
# A tibble: 10 × 8
   variable        median  lower  upper  rhat length ess_bulk ess_tail
   <chr>            <dbl>  <dbl>  <dbl> <dbl>  <dbl>    <dbl>    <dbl>
 1 b_Intercept     23.0   18.2   27.9   1.00    1500    1481.    1347.
 2 b_scalelogDIST   0.989  0.887  1.10  1.00    1500    1352.    1413.
 3 b_scalelogLDIST  1.05   0.937  1.18  0.999   1500    1612.    1579.
 4 b_scalelogAREA   1.21   1.08   1.34  1.00    1500    1490.    1414.
 5 b_fGRAZE2        1.03   0.763  1.34  0.999   1500    1386.    1320.
 6 b_fGRAZE3        1.02   0.789  1.31  1.000   1500    1471.    1491.
 7 b_fGRAZE4        0.995  0.699  1.29  1.00    1500    1412.    1400.
 8 b_fGRAZE5        0.470  0.271  0.757 1.00    1500    1607.    1440.
 9 b_scaleALT       0.999  0.914  1.10  1.000   1500    1587.    1449.
10 b_scaleYR.ISOL   0.994  0.869  1.14  1.000   1500    1523.    1494.
loyn.brm3 |>
  spread_draws(`^b_.*|sigma`, regex = TRUE) |>
  exp() |>
  summarise_draws(
    median,
    HDInterval::hdi,
    rhat,
    length,
    ess_bulk, ess_tail
  )
# A tibble: 11 × 8
   variable         median   lower    upper  rhat length ess_bulk ess_tail
   <chr>             <dbl>   <dbl>    <dbl> <dbl>  <dbl>    <dbl>    <dbl>
 1 b_Intercept      23.0    18.2     27.9   1.00    1500    1494.    1387.
 2 b_scalelogDIST    0.989   0.887    1.10  1.00    1500    1358.    1419.
 3 b_scalelogLDIST   1.05    0.937    1.18  1.000   1500    1620.    1588.
 4 b_scalelogAREA    1.21    1.08     1.34  1.00    1500    1504.    1420.
 5 b_fGRAZE2         1.03    0.763    1.34  1.00    1500    1389.    1364.
 6 b_fGRAZE3         1.02    0.789    1.31  1.00    1500    1479.    1499.
 7 b_fGRAZE4         0.995   0.699    1.29  1.00    1500    1481.    1414.
 8 b_fGRAZE5         0.470   0.271    0.757 1.00    1500    1621.    1454.
 9 b_scaleALT        0.999   0.914    1.10  1.000   1500    1595.    1459.
10 b_scaleYR.ISOL    0.994   0.869    1.14  1.00    1500    1530.    1501.
11 sigma           750.    116.    2945.    0.999   1500    1468.    1381.
loyn.brm3 |> as_draws_df()
# A draws_df: 500 iterations, 3 chains, and 17 variables
   b_Intercept b_scalelogDIST b_scalelogLDIST b_scalelogAREA b_fGRAZE2
1          3.1       -0.02262           0.115           0.20    -0.039
2          3.2       -0.01068           0.027           0.14    -0.163
3          3.1        0.02652           0.044           0.14    -0.049
4          3.2        0.00838          -0.003           0.16    -0.077
5          3.1       -0.02207           0.063           0.17     0.096
6          2.9       -0.05472           0.081           0.16     0.049
7          3.2       -0.06326           0.131           0.18    -0.253
8          3.2       -0.06197           0.036           0.23    -0.029
9          3.2        0.03526           0.059           0.15     0.150
10         3.0       -0.00096          -0.012           0.27     0.127
   b_fGRAZE3 b_fGRAZE4 b_fGRAZE5
1     0.1147     0.211    -0.722
2     0.0029    -0.066    -0.758
3     0.0753    -0.055    -0.664
4    -0.2049    -0.215    -0.785
5     0.1049    -0.081    -0.788
6     0.2290     0.121    -0.039
7     0.0737     0.031    -0.824
8     0.0032    -0.276    -0.965
9    -0.0777     0.022    -1.232
10    0.2332    -0.090    -0.478
# ... with 1490 more draws, and 9 more variables
# ... hidden reserved variables {'.chain', '.iteration', '.draw'}
loyn.brm3 |>
  as_draws_df() |>
  dplyr::select(matches("^b_.*|^sigma$")) |>
  mutate(across(everything(), exp)) |>
  summarise_draws(
    "median",
    ~ HDInterval::hdi(.x),
    "rhat",
    "ess_bulk"
  )
Warning: Dropping 'draws_df' class as required metadata was removed.
# A tibble: 11 × 6
   variable         median   lower    upper  rhat ess_bulk
   <chr>             <dbl>   <dbl>    <dbl> <dbl>    <dbl>
 1 b_Intercept      23.0    18.2     27.9   1.00     1481.
 2 b_scalelogDIST    0.989   0.887    1.10  1.00     1352.
 3 b_scalelogLDIST   1.05    0.937    1.18  0.999    1612.
 4 b_scalelogAREA    1.21    1.08     1.34  1.00     1490.
 5 b_fGRAZE2         1.03    0.763    1.34  0.999    1387.
 6 b_fGRAZE3         1.02    0.789    1.31  1.000    1472.
 7 b_fGRAZE4         0.995   0.699    1.29  1.00     1412.
 8 b_fGRAZE5         0.470   0.271    0.757 1.00     1607.
 9 b_scaleALT        0.999   0.914    1.10  1.000    1587.
10 b_scaleYR.ISOL    0.994   0.869    1.14  1.000    1523.
11 sigma           750.    116.    2945.    1.00     1447.
loyn.brm3$fit |>
  tidyMCMC(
    estimate.method = "median",
    conf.int = TRUE, conf.method = "HPDinterval",
    rhat = TRUE, ess = TRUE
  )
# A tibble: 16 × 7
   term             estimate std.error  conf.low conf.high  rhat   ess
   <chr>               <dbl>     <dbl>     <dbl>     <dbl> <dbl> <int>
 1 b_Intercept       3.13       0.108    2.90       3.33   1.00   1488
 2 b_scalelogDIST   -0.00985    0.0546  -0.116      0.0978 1.00   1350
 3 b_scalelogLDIST   0.0431     0.0582  -0.0650     0.161  0.999  1628
 4 b_scalelogAREA    0.189      0.0548   0.0730     0.290  1.00   1495
 5 b_fGRAZE2         0.0261     0.148   -0.253      0.306  1.00   1385
 6 b_fGRAZE3         0.0186     0.131   -0.235      0.269  1.00   1474
 7 b_fGRAZE4        -0.00818    0.152   -0.321      0.277  1.00   1450
 8 b_fGRAZE5        -0.756      0.259   -1.24      -0.229  1.00   1614
 9 b_scaleALT        0.00132    0.0482  -0.0901     0.0979 1.000  1585
10 b_scaleYR.ISOL   -0.00407    0.0702  -0.140      0.133  1.00   1526
11 sigma             6.68       0.726    5.23       8.10   0.998  1423
12 Intercept         2.97       0.0480   2.87       3.06   0.999  1650
13 prior_Intercept   3.40       0.0976   3.19       3.58   0.999  1313
14 prior_b          -0.0343     1.47    -2.85       2.86   1.00   1202
15 prior_sigma       1.64       2.25     0.00735    4.46   1.000  1427
16 lprior          -25.0        2.04   -29.0      -21.2    0.999  1629

Conclusions:

  • the estimated intercept (expected bird abundance when grazing intensity is equal to 1 and all of the continuous predictors are at their respective averages) is NA. This is the mean of the posterior distribution for this parameter. If we back-transform this to the response scale, this becomes NA.
  • the estimated slope associated with DIST (rate at which the abundance of birds changes per 1 unit change in log-transformed Distance to nearest patch holding other continuous predictors constant and grazing intensity at level 1), is NA (mean) or -0.12 with a standard deviation of -0.01. The 90% credibility intervals indicate that we are 90% confident that the slope is between NA and 0.1 - e.g. there is no evidence of a significant trend.
  • associated with each of the continuous predictors is a partial slope. Each partial slope is the rate of change between bird abundance and the associated predictor (on the log scale due to the link and based on 1 unit change in the predictor on the scale of the predictor). For example, for every one unit change in centred log patch Area, bird abundance is expected to increase by (log) NA.
  • if we back transform this slope (by exponentiation), we get a partial slope for centred log Area of NA. This is interpreted as - for every 1 unit increase in (scaled log) Area, the bird abundance is expected to increase NA fold. That is, there is a NA percent increase per 1 unit increase in centred log Area.
  • Rhat and number of effective samples for each parameter are also provided as MCMC diagnostics and all look good.
loyn.brm3 |> get_variables()
 [1] "b_Intercept"     "b_scalelogDIST"  "b_scalelogLDIST" "b_scalelogAREA" 
 [5] "b_fGRAZE2"       "b_fGRAZE3"       "b_fGRAZE4"       "b_fGRAZE5"      
 [9] "b_scaleALT"      "b_scaleYR.ISOL"  "sigma"           "Intercept"      
[13] "prior_Intercept" "prior_b"         "prior_sigma"     "lprior"         
[17] "lp__"            "accept_stat__"   "stepsize__"      "treedepth__"    
[21] "n_leapfrog__"    "divergent__"     "energy__"       
loyn.draw <- loyn.brm3 |> gather_draws(`^b_.*`, regex = TRUE)
loyn.draw
# A tibble: 15,000 × 5
# Groups:   .variable [10]
   .chain .iteration .draw .variable   .value
    <int>      <int> <int> <chr>        <dbl>
 1      1          1     1 b_Intercept   3.06
 2      1          2     2 b_Intercept   3.15
 3      1          3     3 b_Intercept   3.08
 4      1          4     4 b_Intercept   3.17
 5      1          5     5 b_Intercept   3.11
 6      1          6     6 b_Intercept   2.90
 7      1          7     7 b_Intercept   3.15
 8      1          8     8 b_Intercept   3.20
 9      1          9     9 b_Intercept   3.22
10      1         10    10 b_Intercept   2.99
# ℹ 14,990 more rows
loyn.brm3 |> mcmc_plot(type = "intervals")

loyn.brm3 |>
  gather_draws(`^b_.*`, regex = TRUE) |>
  filter(.variable != "b_Intercept") |>
  ggplot() +
  stat_halfeye(aes(x = .value, y = .variable)) +
  facet_wrap(~.variable, scales = "free")

loyn.brm3 |>
  gather_draws(`^b_.*`, regex = TRUE) |>
  mutate(.value = exp(.value)) |>
  filter(.variable != "b_Intercept") |>
  ggplot() +
  stat_halfeye(aes(x = .value, y = .variable)) +
  geom_vline(xintercept = 1, linetype = "dashed") +
  scale_x_continuous(trans = scales::log2_trans()) +
  theme_classic()

loyn.brm3 |>
  gather_draws(`^b_.*`, regex = TRUE) |>
  filter(.variable != "b_Intercept") |>
  ggplot() +
  geom_density_ridges(aes(x = .value, y = .variable), alpha = 0.4) +
  geom_vline(xintercept = 0, linetype = "dashed")
Picking joint bandwidth of 0.0222

## Or on a fractional scale
loyn.brm3 |>
  gather_draws(`^b_.*`, regex = TRUE) |>
  filter(.variable != "b_Intercept") |>
  ggplot() +
  geom_density_ridges_gradient(
    aes(
      x = exp(.value),
      y = .variable,
      fill = stat(x)
    ),
    alpha = 0.4, colour = "white",
    quantile_lines = TRUE,
    quantiles = c(0.025, 0.975)
  ) +
  geom_vline(xintercept = 1, linetype = "dashed") +
  scale_x_continuous(trans = scales::log2_trans()) +
  scale_fill_viridis_c(option = "C")
Picking joint bandwidth of 0.032

loyn.brm3 |> spread_draws(`^b_.*`, regex = TRUE)
# A tibble: 1,500 × 13
   .chain .iteration .draw b_Intercept b_scalelogDIST b_scalelogLDIST
    <int>      <int> <int>       <dbl>          <dbl>           <dbl>
 1      1          1     1        3.06      -0.0226           0.115  
 2      1          2     2        3.15      -0.0107           0.0273 
 3      1          3     3        3.08       0.0265           0.0438 
 4      1          4     4        3.17       0.00838         -0.00298
 5      1          5     5        3.11      -0.0221           0.0630 
 6      1          6     6        2.90      -0.0547           0.0807 
 7      1          7     7        3.15      -0.0633           0.131  
 8      1          8     8        3.20      -0.0620           0.0365 
 9      1          9     9        3.22       0.0353           0.0591 
10      1         10    10        2.99      -0.000956        -0.0117 
# ℹ 1,490 more rows
# ℹ 7 more variables: b_scalelogAREA <dbl>, b_fGRAZE2 <dbl>, b_fGRAZE3 <dbl>,
#   b_fGRAZE4 <dbl>, b_fGRAZE5 <dbl>, b_scaleALT <dbl>, b_scaleYR.ISOL <dbl>
loyn.brm3 |>
  posterior_samples() |>
  as_tibble()
Warning: Method 'posterior_samples' is deprecated. Please see ?as_draws for
recommended alternatives.
# A tibble: 1,500 × 17
   b_Intercept b_scalelogDIST b_scalelogLDIST b_scalelogAREA b_fGRAZE2 b_fGRAZE3
         <dbl>          <dbl>           <dbl>          <dbl>     <dbl>     <dbl>
 1        3.06      -0.0226           0.115            0.202   -0.0395   0.115  
 2        3.15      -0.0107           0.0273           0.138   -0.163    0.00286
 3        3.08       0.0265           0.0438           0.141   -0.0495   0.0753 
 4        3.17       0.00838         -0.00298          0.161   -0.0770  -0.205  
 5        3.11      -0.0221           0.0630           0.173    0.0958   0.105  
 6        2.90      -0.0547           0.0807           0.157    0.0494   0.229  
 7        3.15      -0.0633           0.131            0.185   -0.253    0.0737 
 8        3.20      -0.0620           0.0365           0.230   -0.0286   0.00319
 9        3.22       0.0353           0.0591           0.146    0.150   -0.0777 
10        2.99      -0.000956        -0.0117           0.268    0.127    0.233  
# ℹ 1,490 more rows
# ℹ 11 more variables: b_fGRAZE4 <dbl>, b_fGRAZE5 <dbl>, b_scaleALT <dbl>,
#   b_scaleYR.ISOL <dbl>, sigma <dbl>, Intercept <dbl>, prior_Intercept <dbl>,
#   prior_b <dbl>, prior_sigma <dbl>, lprior <dbl>, lp__ <dbl>
loyn.brm3 |>
  bayes_R2(summary = FALSE) |>
  median_hdci()
          y      ymin      ymax .width .point .interval
1 0.6149029 0.4872572 0.6968602   0.95 median      hdci

Region of Practical Equivalence

For standardised parameter (negligible effect)

0.1 * sd(log(loyn$ABUND))
[1] 0.09024351
loyn.brm3 |> bayestestR::rope_range()
[1] -0.01  0.01
loyn.brm3 |> bayestestR::rope(range = c(-0.09, 0.09))
# Proportion of samples inside the ROPE [-0.09, 0.09]:

Parameter     | inside ROPE
---------------------------
Intercept     |      0.00 %
scalelogDIST  |     94.10 %
scalelogLDIST |     80.97 %
scalelogAREA  |      1.62 %
fGRAZE2       |     47.89 %
fGRAZE3       |     54.07 %
fGRAZE4       |     48.81 %
fGRAZE5       |      0.00 %
scaleALT      |     98.46 %
scaleYR.ISOL  |     84.34 %
loyn.brm3 |>
  bayestestR::rope(range = c(-0.09, 0.09)) |>
  plot(data = loyn.brm3)

10 Further analyses

loyn.rstanarm4a <- update(loyn.rstanarm3, . ~ scale(log(DIST)) * scale(log(LDIST)),
  diagnostic_file = file.path(tempdir(), "dfa.csv")
)
loyn.rstanarm4b <- update(loyn.rstanarm3, . ~ scale(log(AREA)) * fGRAZE,
  diagnostic_file = file.path(tempdir(), "dfb.csv")
)
loyn.rstanarm4c <- update(loyn.rstanarm3, . ~ scale(log(AREA)) * fGRAZE * scale(YR.ISOL),
  diagnostic_file = file.path(tempdir(), "dfc.csv")
)
loyn.rstanarm4d <- update(loyn.rstanarm3, . ~ scale(ALT),
  diagnostic_file = file.path(tempdir(), "dfd.csv")
)
loyn.rstanarm4e <- update(loyn.rstanarm3, . ~ 1,
  diagnostic_file = file.path(tempdir(), "dfe.csv")
)
loo_compare(
  loo(loyn.rstanarm4a),
  loo(loyn.rstanarm4e)
)
                elpd_diff se_diff
loyn.rstanarm4e  0.0       0.0   
loyn.rstanarm4a -2.4       1.5   
loo_compare(
  loo(loyn.rstanarm4b),
  loo(loyn.rstanarm4e)
)
Warning: Found 2 observation(s) with a pareto_k > 0.7. We recommend calling 'loo' again with argument 'k_threshold = 0.7' in order to calculate the ELPD without the assumption that these observations are negligible. This will refit the model 2 times to compute the ELPDs for the problematic observations directly.
                elpd_diff se_diff
loyn.rstanarm4b   0.0       0.0  
loyn.rstanarm4e -30.4       7.2  
loo_compare(
  loo(loyn.rstanarm4c),
  loo(loyn.rstanarm4e)
)
Warning: Found 8 observation(s) with a pareto_k > 0.7. We recommend calling 'loo' again with argument 'k_threshold = 0.7' in order to calculate the ELPD without the assumption that these observations are negligible. This will refit the model 8 times to compute the ELPDs for the problematic observations directly.
                elpd_diff se_diff
loyn.rstanarm4c   0.0       0.0  
loyn.rstanarm4e -22.0       6.8  
loo_compare(
  loo(loyn.rstanarm4d),
  loo(loyn.rstanarm4e)
)
                elpd_diff se_diff
loyn.rstanarm4d  0.0       0.0   
loyn.rstanarm4e -3.6       2.3   
bayes_factor(
  bridge_sampler(loyn.rstanarm4a),
  bridge_sampler(loyn.rstanarm4e)
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of x1 over x2: 0.00138
bayes_factor(
  bridge_sampler(loyn.rstanarm4b),
  bridge_sampler(loyn.rstanarm4e)
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of x1 over x2: 1032891552.95408
bayes_factor(
  bridge_sampler(loyn.rstanarm4c),
  bridge_sampler(loyn.rstanarm4e)
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Estimated Bayes factor in favor of x1 over x2: 7541.08718
bayes_factor(
  bridge_sampler(loyn.rstanarm4d),
  bridge_sampler(loyn.rstanarm4e)
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Estimated Bayes factor in favor of x1 over x2: 5.03167
loyn.brm4a <- update(loyn.brm3, . ~ scale(log(DIST)) * scale(log(LDIST)),
  save_pars = save_pars(all = TRUE), refresh = 0
)
Start sampling
loyn.brm4b <- update(loyn.brm3, . ~ scale(log(AREA)) * fGRAZE,
  save_pars = save_pars(all = TRUE), refresh = 0
)
Start sampling
loyn.brm4c <- update(loyn.brm3, . ~ scale(log(AREA)) * fGRAZE * scale(YR.ISOL),
  save_pars = save_pars(all = TRUE), refresh = 0
)
Start sampling
loyn.brm4d <- update(loyn.brm3, . ~ scale(ALT),
  save_pars = save_pars(all = TRUE), refresh = 0
)
Start sampling
loyn.brm4e <- update(loyn.brm3, . ~ 1,
  save_pars = save_pars(all = TRUE), refresh = 0
)
The desired updates require recompiling the model
Compiling Stan program...
Start sampling
waic(loyn.brm4a)
Warning: 
2 (3.6%) p_waic estimates greater than 0.4. We recommend trying loo instead.

Computed from 1500 by 56 log-likelihood matrix.

          Estimate  SE
elpd_waic   -217.0 4.4
p_waic         3.8 0.7
waic         433.9 8.8

2 (3.6%) p_waic estimates greater than 0.4. We recommend trying loo instead. 
loo(loyn.brm4a)

Computed from 1500 by 56 log-likelihood matrix.

         Estimate  SE
elpd_loo   -217.1 4.4
p_loo         3.9 0.8
looic       434.1 8.9
------
MCSE of elpd_loo is 0.1.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.8, 1.1]).

All Pareto k estimates are good (k < 0.69).
See help('pareto-k-diagnostic') for details.
loo(loyn.brm4e)

Computed from 1500 by 56 log-likelihood matrix.

         Estimate  SE
elpd_loo   -215.2 4.5
p_loo         1.2 0.2
looic       430.4 9.1
------
MCSE of elpd_loo is 0.0.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.9, 1.1]).

All Pareto k estimates are good (k < 0.69).
See help('pareto-k-diagnostic') for details.
loo_compare(
  loo(loyn.brm4a),
  loo(loyn.brm4e)
)
           elpd_diff se_diff
loyn.brm4e  0.0       0.0   
loyn.brm4a -1.9       1.2   
loo_compare(
  loo(loyn.brm4b),
  loo(loyn.brm4e)
)
Warning: Found 3 observations with a pareto_k > 0.7 in model 'loyn.brm4b'. We
recommend to set 'moment_match = TRUE' in order to perform moment matching for
problematic observations.
           elpd_diff se_diff
loyn.brm4b   0.0       0.0  
loyn.brm4e -29.5       7.1  
loo_compare(
  loo(loyn.brm4b, moment_match = TRUE),
  loo(loyn.brm4e)
)
           elpd_diff se_diff
loyn.brm4b   0.0       0.0  
loyn.brm4e -29.6       7.0  
loo_compare(
  loo(loyn.brm4c, moment_match = TRUE),
  loo(loyn.brm4e)
)
Warning: Found 2 observations with a pareto_k > 0.69 in model 'loyn.brm4c'. We
recommend to run more iterations to get at least about 2200 posterior draws to
improve LOO-CV approximation accuracy.
           elpd_diff se_diff
loyn.brm4c   0.0       0.0  
loyn.brm4e -20.5       6.8  
loo_compare(
  loo(loyn.brm4d),
  loo(loyn.brm4e)
)
           elpd_diff se_diff
loyn.brm4d  0.0       0.0   
loyn.brm4e -3.5       2.0   

An alternative is to compute Bayes factors based on bridge sampling. Note, this process usually requires far greater number of posterior samples (10x) in order to be stable. It is also advisable to run this multiple times to ensure stability.

It calculates the marginal likelihood of one model in favour of another. The larger the value, the more evidence there is of one model over the other.

bayes_factor(
  loyn.brm4a,
  loyn.brm4e
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4a over loyn.brm4e: 0.00019
# OR
bayes_factor(
  loyn.brm4e,
  loyn.brm4a
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4e over loyn.brm4a: 5208.20501
bayes_factor(
  loyn.brm4b,
  loyn.brm4e
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4b over loyn.brm4e: 51747.45102
# OR
bayes_factor(
  loyn.brm4e,
  loyn.brm4b
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4e over loyn.brm4b: 0.00002
bayes_factor(
  loyn.brm4c,
  loyn.brm4e
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4c over loyn.brm4e: 0.00571
# OR
bayes_factor(
  loyn.brm4e,
  loyn.brm4c
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Estimated Bayes factor in favor of loyn.brm4e over loyn.brm4c: 184.26158
bayes_factor(
  loyn.brm4d,
  loyn.brm4e
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Estimated Bayes factor in favor of loyn.brm4d over loyn.brm4e: 1.71869
# OR
bayes_factor(
  loyn.brm4e,
  loyn.brm4d
)
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Iteration: 6
Estimated Bayes factor in favor of loyn.brm4e over loyn.brm4d: 0.58924

Compare effect of grazing at different patch areas

loyn.list <- with(loyn, list(AREA = c(min(AREA), mean(AREA), max(AREA))))

loyn.brm4b |>
  emmeans(~ fGRAZE | AREA, at = loyn.list, type = "response") |>
  pairs(reverse = FALSE)
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
AREA = 0.1:
 contrast          ratio lower.HPD upper.HPD
 fGRAZE1 / fGRAZE2 1.871   0.80606     3.351
 fGRAZE1 / fGRAZE3 1.987   0.89209     3.595
 fGRAZE1 / fGRAZE4 7.001   0.94945    32.270
 fGRAZE1 / fGRAZE5 4.383   1.24492    10.166
 fGRAZE2 / fGRAZE3 1.089   0.36584     2.013
 fGRAZE2 / fGRAZE4 3.631   0.32763    17.765
 fGRAZE2 / fGRAZE5 2.360   0.54165     5.560
 fGRAZE3 / fGRAZE4 3.502   0.29180    15.408
 fGRAZE3 / fGRAZE5 2.157   0.57014     5.350
 fGRAZE4 / fGRAZE5 0.621   0.03363     2.665

AREA = 69.2696428571429:
 contrast          ratio lower.HPD upper.HPD
 fGRAZE1 / fGRAZE2 0.912   0.70844     1.143
 fGRAZE1 / fGRAZE3 0.870   0.65783     1.121
 fGRAZE1 / fGRAZE4 0.544   0.24250     0.961
 fGRAZE1 / fGRAZE5 1.636   0.63787     3.201
 fGRAZE2 / fGRAZE3 0.952   0.66152     1.277
 fGRAZE2 / fGRAZE4 0.596   0.25687     1.043
 fGRAZE2 / fGRAZE5 1.790   0.70721     3.603
 fGRAZE3 / fGRAZE4 0.620   0.27817     1.133
 fGRAZE3 / fGRAZE5 1.858   0.80895     3.925
 fGRAZE4 / fGRAZE5 3.020   0.71219     6.923

AREA = 1771:
 contrast          ratio lower.HPD upper.HPD
 fGRAZE1 / fGRAZE2 0.644   0.33566     1.041
 fGRAZE1 / fGRAZE3 0.574   0.29040     1.004
 fGRAZE1 / fGRAZE4 0.155   0.00756     0.610
 fGRAZE1 / fGRAZE5 0.986   0.08815     3.537
 fGRAZE2 / fGRAZE3 0.890   0.32607     1.776
 fGRAZE2 / fGRAZE4 0.243   0.01129     0.961
 fGRAZE2 / fGRAZE5 1.527   0.08901     6.142
 fGRAZE3 / fGRAZE4 0.263   0.00862     1.132
 fGRAZE3 / fGRAZE5 1.693   0.14066     7.174
 fGRAZE4 / fGRAZE5 6.629   0.05231    44.263

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
newdata <- loyn.brm4b |>
  emmeans(~ fGRAZE | AREA, at = loyn.list, type = "response") |>
  pairs() |>
  as.data.frame()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
head(newdata)
 contrast          AREA    ratio lower.HPD upper.HPD
 fGRAZE1 / fGRAZE2 0.1  1.870645 0.8060627   3.35084
 fGRAZE1 / fGRAZE3 0.1  1.987118 0.8920862   3.59480
 fGRAZE1 / fGRAZE4 0.1  7.001174 0.9494467  32.27003
 fGRAZE1 / fGRAZE5 0.1  4.382613 1.2449219  10.16644
 fGRAZE2 / fGRAZE3 0.1  1.088611 0.3658380   2.01329
 fGRAZE2 / fGRAZE4 0.1  3.631306 0.3276338  17.76537

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
newdata <- loyn.brm4b |>
  emmeans(~ fGRAZE | AREA, at = loyn.list, type = "response") |>
  pairs() |>
  gather_emmeans_draws()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
newdata |>
  median_hdci() |>
  ggplot() +
  geom_hline(yintercept = 1, linetype = "dashed") +
  geom_pointrange(aes(y = .value, ymin = .lower, ymax = .upper, x = contrast)) +
  facet_wrap(~AREA) +
  coord_flip()

loyn.brm4b |>
  emmeans(~ fGRAZE | AREA, at = loyn.list, type = "response") |>
  gather_emmeans_draws()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
# A tibble: 22,500 × 6
# Groups:   fGRAZE, AREA [15]
   fGRAZE  AREA .chain .iteration .draw .value
   <fct>  <dbl>  <int>      <int> <int>  <dbl>
 1 1        0.1     NA         NA     1   3.13
 2 1        0.1     NA         NA     2   2.85
 3 1        0.1     NA         NA     3   2.60
 4 1        0.1     NA         NA     4   2.86
 5 1        0.1     NA         NA     5   3.15
 6 1        0.1     NA         NA     6   2.81
 7 1        0.1     NA         NA     7   2.89
 8 1        0.1     NA         NA     8   2.89
 9 1        0.1     NA         NA     9   3.08
10 1        0.1     NA         NA    10   3.26
# ℹ 22,490 more rows
newdata.p <- newdata |> summarise(P = mean(.value > 1))
`summarise()` has grouped output by 'contrast'. You can override using the
`.groups` argument.
g <- newdata |>
  ggplot() +
  geom_vline(xintercept = 1, linetype = "dashed") +
  stat_slab(aes(
    x = .value, y = contrast,
    fill = stat(ggdist::cut_cdf_qi(cdf,
      .width = c(0.5, 0.8, 0.95),
      labels = scales::percent_format()
    ))
  ), color = "black") +
  scale_fill_brewer("Interval", direction = -1, na.translate = FALSE) +
  facet_grid(~ round(AREA, 1))
g + geom_text(data = newdata.p, aes(y = contrast, x = 1, label = paste("P = ", round(P, 3))), hjust = -0.2, position = position_nudge(y = 0.5))

11 Summary figure

loyn.list <- with(loyn, list(AREA = modelr::seq_range(AREA, n = 100)))

newdata <- emmeans(loyn.brm3, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  as.data.frame()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
head(newdata)
     AREA fGRAZE response lower.HPD upper.HPD
  0.10000 1      14.96539  9.278769  22.25754
 17.98788 1      25.32960 20.696223  30.37126
 35.87576 1      27.17799 22.610411  31.71495
 53.76364 1      28.28795 23.928452  32.82209
 71.65152 1      29.17646 24.539469  33.59735
 89.53939 1      29.80402 25.106082  34.28127

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
ggplot(newdata, aes(y = response, x = AREA)) +
  geom_ribbon(aes(ymin = lower.HPD, ymax = upper.HPD, fill = fGRAZE), alpha = 0.3) +
  geom_line(aes(color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

spaghetti <- emmeans(loyn.rstanarm3, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  gather_emmeans_draws() |>
  mutate(Fit = exp(.value))
wch <- sample(1:max(spaghetti$.draw), 100, replace = FALSE)
spaghetti <- spaghetti |>
  filter(.draw %in% wch)
ggplot(newdata) +
  geom_line(data = spaghetti, aes(
    y = Fit, x = AREA, color = fGRAZE,
    group = interaction(fGRAZE, .draw)
  ), alpha = 0.05) +
  geom_line(aes(y = response, x = AREA, color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

## or honouring the data range
loyn.nd <- loyn |>
  group_by(fGRAZE) |>
  tidyr::expand(
    AREA = modelr::seq_range(AREA, n = 100),
    DIST = mean(DIST), LDIST = mean(LDIST), ALT = mean(ALT), YR.ISOL = mean(YR.ISOL)
  )
loyn.rstanarm3 |>
  epred_draws(newdata = loyn.nd, value = ".value") |>
  median_hdci() |>
  ggplot(aes(x = AREA, y = .value, colour = fGRAZE, fill = fGRAZE)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), colour = NA, alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10() +
  scale_x_log10() +
  theme_bw()

loyn.list <- with(loyn, list(AREA = seq(min(AREA), max(AREA), len = 100)))

newdata <- emmeans(loyn.rstanarm4b, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  as.data.frame()
head(newdata)
     AREA fGRAZE response lower.HPD upper.HPD
  0.10000 1      19.03816  11.62715  26.90043
 17.98788 1      26.16248  22.53196  29.96777
 35.87576 1      27.28563  23.98798  30.61037
 53.76364 1      27.95703  24.68977  31.09034
 71.65152 1      28.48415  25.05349  31.40785
 89.53939 1      28.88190  25.37502  31.83853

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
ggplot(newdata, aes(y = response, x = AREA)) +
  geom_ribbon(aes(ymin = lower.HPD, ymax = upper.HPD, fill = fGRAZE), alpha = 0.3) +
  geom_line(aes(color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

spaghetti <- emmeans(loyn.rstanarm4b, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  gather_emmeans_draws() |>
  mutate(Fit = exp(.value))
wch <- sample(1:max(spaghetti$.draw), 100, replace = FALSE)
spaghetti <- spaghetti |> filter(.draw %in% wch)
ggplot(newdata) +
  geom_line(data = spaghetti, aes(
    y = Fit, x = AREA, color = fGRAZE,
    group = interaction(fGRAZE, .draw)
  ), alpha = 0.05) +
  geom_line(aes(y = response, x = AREA, color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

## or honouring the data range
loyn.nd <- loyn |>
  group_by(fGRAZE) |>
  tidyr::expand(AREA = modelr::seq_range(AREA, n = 100))
loyn.rstanarm4b |>
  epred_draws(newdata = loyn.nd, value = ".value") |>
  median_hdci() |>
  ggplot(aes(x = AREA, y = .value, colour = fGRAZE, fill = fGRAZE)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), colour = NA, alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10() +
  scale_x_log10() +
  theme_bw()

loyn.list <- with(loyn, list(AREA = modelr::seq_range(AREA, n = 100)))

newdata <- emmeans(loyn.brm3, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  as.data.frame()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
head(newdata)
     AREA fGRAZE response lower.HPD upper.HPD
  0.10000 1      14.96539  9.278769  22.25754
 17.98788 1      25.32960 20.696223  30.37126
 35.87576 1      27.17799 22.610411  31.71495
 53.76364 1      28.28795 23.928452  32.82209
 71.65152 1      29.17646 24.539469  33.59735
 89.53939 1      29.80402 25.106082  34.28127

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
ggplot(newdata, aes(y = response, x = AREA)) +
  geom_ribbon(aes(ymin = lower.HPD, ymax = upper.HPD, fill = fGRAZE), alpha = 0.3) +
  geom_line(aes(color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

spaghetti <- emmeans(loyn.brm3, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  gather_emmeans_draws() |>
  mutate(Fit = exp(.value))
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
wch <- sample(1:max(spaghetti$.draw), 100, replace = FALSE)
spaghetti <- spaghetti |> filter(.draw %in% wch)
ggplot(newdata) +
  geom_line(data = spaghetti, aes(
    y = Fit, x = AREA, color = fGRAZE,
    group = interaction(fGRAZE, .draw)
  ), alpha = 0.1) +
  geom_line(aes(y = response, x = AREA, color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

# or honouring the data range
loyn.nd <- loyn |>
  group_by(fGRAZE) |>
  tidyr::expand(
    AREA = modelr::seq_range(AREA, n = 100),
    DIST = mean(DIST), LDIST = mean(LDIST), ALT = mean(ALT), YR.ISOL = mean(YR.ISOL)
  )
loyn.brm3 |>
  epred_draws(newdata = loyn.nd, value = ".value") |>
  median_hdci() |>
  ggplot(aes(x = AREA, y = .value, colour = fGRAZE, fill = fGRAZE)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), colour = NA, alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10() +
  scale_x_log10() +
  theme_bw()

Lets explore the relationship between bird abundance and patch area for each grazing intensity separately.

loyn.list <- with(loyn, list(AREA = modelr::seq_range(AREA, n = 100)))

newdata <- emmeans(loyn.brm4b, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  as.data.frame()
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
head(newdata)
     AREA fGRAZE response lower.HPD upper.HPD
  0.10000 1      20.96232  13.99935  29.80539
 17.98788 1      27.37903  23.85716  30.94850
 35.87576 1      28.42268  25.33449  31.77239
 53.76364 1      29.04033  25.87632  32.12257
 71.65152 1      29.45334  26.48695  32.76029
 89.53939 1      29.82220  26.78742  33.13515

Point estimate displayed: median 
Results are back-transformed from the log scale 
HPD interval probability: 0.95 
ggplot(newdata, aes(y = response, x = AREA)) +
  geom_ribbon(aes(ymin = lower.HPD, ymax = upper.HPD, fill = fGRAZE), alpha = 0.3) +
  geom_line(aes(color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

spaghetti <- emmeans(loyn.brm4b, ~ AREA | fGRAZE, at = loyn.list, type = "response") |>
  gather_emmeans_draws() |>
  mutate(Fit = exp(.value))
Warning: Method 'parse_bf' is deprecated. Please use 'brmsterms' instead.
wch <- sample(1:max(spaghetti$.draw), 100, replace = FALSE)
spaghetti <- spaghetti |> filter(.draw %in% wch)
ggplot(newdata) +
  geom_line(data = spaghetti, aes(
    y = Fit, x = AREA, color = fGRAZE,
    group = interaction(fGRAZE, .draw)
  ), alpha = 0.1) +
  geom_line(aes(y = response, x = AREA, color = fGRAZE)) +
  theme_bw() +
  scale_x_log10() +
  scale_y_log10()

## or honouring the data range
loyn.nd <- loyn |>
  group_by(fGRAZE) |>
  tidyr::expand(AREA = modelr::seq_range(AREA, n = 100))
loyn.brm4b |>
  epred_draws(newdata = loyn.nd, value = ".value") |>
  median_hdci() |>
  ggplot(aes(x = AREA, y = .value, colour = fGRAZE, fill = fGRAZE)) +
  geom_ribbon(aes(ymin = .lower, ymax = .upper), colour = NA, alpha = 0.3) +
  geom_line() +
  geom_point(data = loyn, aes(y = ABUND, x = AREA)) +
  scale_y_log10() +
  scale_x_log10() +
  theme_bw()

12 References

Loyn, R. H. 1987. “Nature Conservation: The Role of Remnants of Native Vegetation.” In, edited by D. A. Saunders, G. W. Arnold, A. A. Burbridge, and A. J. M. Hopkins. Chipping Norton, NSW: Surrey Beatty & Sons.